Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gitsunmin] Week 09 Solutions #527

Merged
merged 4 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions find-minimum-in-rotated-sorted-array/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array
* time complexity : O(log n)
* space complexity : O(1)
*/

function findMin(nums: number[]): number {
let left = 0;
let right = nums.length - 1;

while (left < right) {
const mid = Math.floor((left + right) / 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳이 floor은 안사용하셔도 괜찮을 것 같습니다. 이분탐색에서 while문에 등호를 넣거나 if나 else에서 등호를 넣거나 테스트 해보시면 좋을 것 같습니다.


if (nums[mid] > nums[right]) left = mid + 1;
else right = mid;
}

return nums[left];
};
31 changes: 31 additions & 0 deletions linked-list-cycle/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* https://leetcode.com/problems/linked-list-cycle/
* time complexity : O(n)
* space complexity : O(1)
*/

export class ListNode {
val: number
next: ListNode | null
constructor(val?: number, next?: ListNode | null) {
this.val = (val === undefined ? 0 : val)
this.next = (next === undefined ? null : next)
}
}

function hasCycle(head: ListNode | null): boolean {
if (!head || !head.next) {
return false;
}

let slow: ListNode | null = head;
let fast: ListNode | null = head.next;

while (slow !== fast) {
if (!fast || !fast.next) return false;
slow = slow!.next;
fast = fast.next.next;
}

return true;
Comment on lines +16 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드 리뷰를 하면서 드는 생각인데 union-find로도 풀어볼 걸 그랬네요

};
44 changes: 44 additions & 0 deletions pacific-atlantic-water-flow/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* https://leetcode.com/problems/two-sum
* time complexity : O(m x m)
* space complexity : O(m x n)
*/
function pacificAtlantic(heights: number[][]): number[][] {
const m = heights.length;
const n = heights[0].length;
const pacific: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false));
const atlantic: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false));

const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]];

function dfs(r: number, c: number, visited: boolean[][], prevHeight: number) {
if (r < 0 || c < 0 || r >= m || c >= n || visited[r][c] || heights[r][c] < prevHeight) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 귀찮아서 한 줄로 쓰기는 하는데, 리뷰어의 입장에서 코드 스멜이나 단축 평가를 고려하면 적절한 조건들로 나누는 게 좋을 것 같습니다. 이 경우는 index error가 일어나지 않을 r, c를 한정하기 위해 순서가 어쩔 수 없지만...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lymchgmk
이야기 감사합니다! 코드 스멜도 신경써볼게요!

return;
}
visited[r][c] = true;
for (const [dr, dc] of directions) {
dfs(r + dr, c + dc, visited, heights[r][c]);
}
}

for (let i = 0; i < m; i++) {
dfs(i, 0, pacific, heights[i][0]);
dfs(i, n - 1, atlantic, heights[i][n - 1]);
}
for (let i = 0; i < n; i++) {
dfs(0, i, pacific, heights[0][i]);
dfs(m - 1, i, atlantic, heights[m - 1][i]);
}

const result: number[][] = [];

for (let r = 0; r < m; r++) {
for (let c = 0; c < n; c++) {
if (pacific[r][c] && atlantic[r][c]) {
result.push([r, c]);
}
}
}

return result;
}