Skip to content

Commit 061d42a

Browse files
authored
Merge pull request #519 from HC-kang/main
[강희찬] WEEK 9 Solution
2 parents 0228570 + 5d0a623 commit 061d42a

File tree

5 files changed

+314
-0
lines changed

5 files changed

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

linked-list-cycle/HC-kang.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = val === undefined ? 0 : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}
9+
10+
/**
11+
* https://leetcode.com/problems/linked-list-cycle
12+
* T.C. O(n)
13+
* S.C. O(n)
14+
*/
15+
function hasCycle(head: ListNode | null): boolean {
16+
const SET = new Set<ListNode>();
17+
while (head) {
18+
if (SET.has(head)) return true;
19+
SET.add(head);
20+
head = head.next
21+
}
22+
return false;
23+
};
24+
25+
/**
26+
* T.C. O(n)
27+
* S.C. O(1)
28+
*/
29+
function hasCycle(head: ListNode | null): boolean {
30+
let fast = head;
31+
let slow = head;
32+
33+
while (fast && fast.next) {
34+
fast = fast.next.next;
35+
slow = slow!.next;
36+
37+
if (fast === slow) {
38+
return true;
39+
}
40+
}
41+
return false;
42+
};

maximum-subarray/HC-kang.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* https://leetcode.com/problems/maximum-subarray
3+
* Kadane's Algorithm
4+
* T.C. O(n)
5+
* S.C. O(1)
6+
*/
7+
function maxSubArray(nums: number[]): number {
8+
let maxSum = nums[0];
9+
10+
for (let i = 1; i < nums.length; i++) {
11+
nums[i] = Math.max(nums[i], nums[i] + nums[i - 1]);
12+
maxSum = Math.max(maxSum, nums[i]);
13+
}
14+
15+
return maxSum;
16+
}
17+
18+
/**
19+
* Divide and Conquer version
20+
* T.C. O(n log n)
21+
* S.C. O(log n) - call stack
22+
*/
23+
function maxSubArray(nums: number[]): number {
24+
function maxSubArrayHelper(
25+
nums: number[],
26+
left: number,
27+
right: number
28+
): number {
29+
if (left === right) return nums[left];
30+
31+
const mid = (left + right) >> 1;
32+
const leftMax = maxSubArrayHelper(nums, left, mid);
33+
const rightMax = maxSubArrayHelper(nums, mid + 1, right);
34+
let leftSum = -Infinity;
35+
let rightSum = -Infinity;
36+
let sum = 0;
37+
38+
for (let i = mid; i >= left; i--) {
39+
sum += nums[i];
40+
leftSum = Math.max(leftSum, sum);
41+
}
42+
43+
sum = 0;
44+
for (let i = mid + 1; i <= right; i++) {
45+
sum += nums[i];
46+
rightSum = Math.max(rightSum, sum);
47+
}
48+
49+
return Math.max(leftMax, rightMax, leftSum + rightSum);
50+
}
51+
52+
return maxSubArrayHelper(nums, 0, nums.length - 1);
53+
}
54+
55+
/**
56+
* Iterative version
57+
* T.C. O(n log n)
58+
* S.C. O(log n) - call stack
59+
*/
60+
function maxSubArray(nums: number[]): number {
61+
const stack = [[0, nums.length - 1]];
62+
let maxSum = nums[0];
63+
64+
while (stack.length) {
65+
const [left, right] = stack.pop()!;
66+
if (left === right) {
67+
maxSum = Math.max(maxSum, nums[left]);
68+
continue;
69+
}
70+
71+
const mid = (left + right) >> 1;
72+
stack.push([left, mid], [mid + 1, right]);
73+
74+
let leftSum = -Infinity;
75+
let rightSum = -Infinity;
76+
let sum = 0;
77+
78+
for (let i = mid; i >= left; i--) {
79+
sum += nums[i];
80+
leftSum = Math.max(leftSum, sum);
81+
}
82+
83+
sum = 0;
84+
for (let i = mid + 1; i <= right; i++) {
85+
sum += nums[i];
86+
rightSum = Math.max(rightSum, sum);
87+
}
88+
89+
maxSum = Math.max(maxSum, leftSum + rightSum);
90+
}
91+
92+
return maxSum;
93+
}

minimum-window-substring/HC-kang.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* https://leetcode.com/problems/minimum-window-substring
3+
* T.C. O(s + t)
4+
* S.C. O(t)
5+
*/
6+
function minWindow(s: string, t: string): string {
7+
let minLow = 0;
8+
let minHigh = s.length;
9+
10+
const counts: Record<string, number> = {};
11+
for (const c of t) {
12+
counts[c] = (counts[c] || 0) + 1;
13+
}
14+
15+
let included = 0;
16+
17+
let low = 0;
18+
for (let high = 0; high < s.length; high++) {
19+
if (counts[s[high]]) {
20+
if (counts[s[high]] > 0) {
21+
included++;
22+
}
23+
counts[s[high]]--;
24+
}
25+
26+
while (included === t.length) {
27+
if (high - low < minHigh - minLow) {
28+
minLow = low;
29+
minHigh = high;
30+
}
31+
32+
if (counts[s[low]]) {
33+
counts[s[low]]++;
34+
if (counts[s[low]] > 0) {
35+
included--;
36+
}
37+
}
38+
39+
low++;
40+
}
41+
}
42+
43+
return minHigh === s.length ? '' : s.substring(minLow, minHigh + 1);
44+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* https://leetcode.com/problems/pacific-atlantic-water-flow
3+
* T.C. O((m * n)^2)
4+
* S.C. O(m * n)
5+
*/
6+
function pacificAtlantic(heights: number[][]): number[][] {
7+
const dir = [
8+
[0, 1],
9+
[0, -1],
10+
[1, 0],
11+
[-1, 0],
12+
];
13+
14+
function pacificDfs(row: number, col: number, visited: Set<string>) {
15+
const key = `${row},${col}`;
16+
if (visited.has(key)) return;
17+
visited.add(key);
18+
19+
if (row === 0 || col === 0) {
20+
// left or top
21+
return true;
22+
}
23+
24+
for (let [r, c] of dir) {
25+
if (row + r < 0 || row + r >= heights.length) continue;
26+
if (col + c < 0 || col + c >= heights[0].length) continue;
27+
if (heights[row][col] < heights[row + r][col + c]) continue;
28+
if (pacificDfs(row + r, col + c, visited)) return true;
29+
}
30+
return false;
31+
}
32+
33+
function atlanticDfs(row: number, col: number, visited: Set<string>) {
34+
const key = `${row},${col}`;
35+
if (visited.has(key)) return;
36+
visited.add(key);
37+
38+
if (row === heights.length - 1 || col === heights[0].length - 1) {
39+
// right or bottom
40+
return true;
41+
}
42+
43+
for (let [r, c] of dir) {
44+
if (row + r < 0 || row + r >= heights.length) continue;
45+
if (col + c < 0 || col + c >= heights[0].length) continue;
46+
if (heights[row][col] < heights[row + r][col + c]) continue;
47+
if (atlanticDfs(row + r, col + c, visited)) return true;
48+
}
49+
return false;
50+
}
51+
52+
const result: number[][] = [];
53+
for (let i = 0; i < heights.length; i++) {
54+
for (let j = 0; j < heights[0].length; j++) {
55+
if (
56+
pacificDfs(i, j, new Set<string>()) &&
57+
atlanticDfs(i, j, new Set<string>())
58+
) {
59+
result.push([i, j]);
60+
}
61+
}
62+
}
63+
64+
return result;
65+
}
66+
67+
/**
68+
* T.C. O(m * n)
69+
* S.C. O(m * n)
70+
*/
71+
function pacificAtlantic(heights: number[][]): number[][] {
72+
const pacific: Set<string> = new Set();
73+
const atlantic: Set<string> = new Set();
74+
75+
const dir = [
76+
[0, 1],
77+
[0, -1],
78+
[1, 0],
79+
[-1, 0],
80+
];
81+
82+
function dfs(row: number, col: number, visited: Set<string>) {
83+
const key = `${row},${col}`;
84+
if (visited.has(key)) return;
85+
visited.add(key);
86+
87+
for (let [r, c] of dir) {
88+
if (row + r < 0 || row + r >= heights.length) continue;
89+
if (col + c < 0 || col + c >= heights[0].length) continue;
90+
if (heights[row][col] > heights[row + r][col + c]) continue;
91+
dfs(row + r, col + c, visited);
92+
}
93+
}
94+
95+
for (let i = 0; i < heights.length; i++) {
96+
dfs(i, 0, pacific);
97+
dfs(i, heights[0].length - 1, atlantic);
98+
}
99+
100+
for (let i = 0; i < heights[0].length; i++) {
101+
dfs(0, i, pacific);
102+
dfs(heights.length - 1, i, atlantic);
103+
}
104+
105+
const result: number[][] = [];
106+
107+
for (const p of pacific) {
108+
if (atlantic.has(p)) {
109+
const [row, col] = p.split(',').map(Number);
110+
result.push([row, col]);
111+
}
112+
}
113+
114+
return result;
115+
}

0 commit comments

Comments
 (0)