Skip to content

Commit 0228570

Browse files
authored
Merge pull request #527 from gitsunmin/main
[gitsunmin] Week 09 Solutions
2 parents 0ef1547 + 6868da5 commit 0228570

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array
3+
* time complexity : O(log n)
4+
* space complexity : O(1)
5+
*/
6+
7+
function findMin(nums: number[]): number {
8+
let left = 0;
9+
let right = nums.length - 1;
10+
11+
while (left < right) {
12+
const mid = Math.floor((left + right) / 2);
13+
14+
if (nums[mid] > nums[right]) left = mid + 1;
15+
else right = mid;
16+
}
17+
18+
return nums[left];
19+
};

linked-list-cycle/gitsunmin.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* https://leetcode.com/problems/linked-list-cycle/
3+
* time complexity : O(n)
4+
* space complexity : O(1)
5+
*/
6+
7+
export class ListNode {
8+
val: number
9+
next: ListNode | null
10+
constructor(val?: number, next?: ListNode | null) {
11+
this.val = (val === undefined ? 0 : val)
12+
this.next = (next === undefined ? null : next)
13+
}
14+
}
15+
16+
function hasCycle(head: ListNode | null): boolean {
17+
if (!head || !head.next) {
18+
return false;
19+
}
20+
21+
let slow: ListNode | null = head;
22+
let fast: ListNode | null = head.next;
23+
24+
while (slow !== fast) {
25+
if (!fast || !fast.next) return false;
26+
slow = slow!.next;
27+
fast = fast.next.next;
28+
}
29+
30+
return true;
31+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* https://leetcode.com/problems/two-sum
3+
* time complexity : O(m x m)
4+
* space complexity : O(m x n)
5+
*/
6+
function pacificAtlantic(heights: number[][]): number[][] {
7+
const m = heights.length;
8+
const n = heights[0].length;
9+
const pacific: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false));
10+
const atlantic: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false));
11+
12+
const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]];
13+
14+
function dfs(r: number, c: number, visited: boolean[][], prevHeight: number) {
15+
if (r < 0 || c < 0 || r >= m || c >= n || visited[r][c] || heights[r][c] < prevHeight) {
16+
return;
17+
}
18+
visited[r][c] = true;
19+
for (const [dr, dc] of directions) {
20+
dfs(r + dr, c + dc, visited, heights[r][c]);
21+
}
22+
}
23+
24+
for (let i = 0; i < m; i++) {
25+
dfs(i, 0, pacific, heights[i][0]);
26+
dfs(i, n - 1, atlantic, heights[i][n - 1]);
27+
}
28+
for (let i = 0; i < n; i++) {
29+
dfs(0, i, pacific, heights[0][i]);
30+
dfs(m - 1, i, atlantic, heights[m - 1][i]);
31+
}
32+
33+
const result: number[][] = [];
34+
35+
for (let r = 0; r < m; r++) {
36+
for (let c = 0; c < n; c++) {
37+
if (pacific[r][c] && atlantic[r][c]) {
38+
result.push([r, c]);
39+
}
40+
}
41+
}
42+
43+
return result;
44+
}

0 commit comments

Comments
 (0)