Skip to content

Commit 790b428

Browse files
authored
Merge pull request #1521 from Jeehay28/main
[Jeehay28] Week 09 Solutions
2 parents bc85846 + 3725aff commit 790b428

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed

linked-list-cycle/Jeehay28.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
// TC: O(n)
11+
// SC: O(1)
12+
function hasCycle(head: ListNode | null): boolean {
13+
14+
let fast: ListNode | null = head;
15+
let slow: ListNode | null = head;
16+
17+
while (fast && fast.next) {
18+
fast = fast.next.next;
19+
20+
if (slow !== null) {
21+
slow = slow.next;
22+
} else {
23+
return false;
24+
}
25+
26+
if (fast === slow) return true;
27+
}
28+
29+
return false;
30+
}
31+
32+
33+
// TC: O(n)
34+
// SC: O(n)
35+
/*
36+
function hasCycle(head: ListNode | null): boolean {
37+
38+
let dummy: ListNode | null = head;
39+
const visited = new Set<ListNode>();
40+
41+
while (dummy) {
42+
if (visited.has(dummy)) return true;
43+
44+
visited.add(dummy);
45+
46+
dummy = dummy.next;
47+
}
48+
49+
return false;
50+
}
51+
*/
52+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
function maxProduct(nums: number[]): number {
4+
let prevMax = nums[0];
5+
let prevMin = nums[0];
6+
let maxProd = nums[0];
7+
8+
for (let i = 1; i < nums.length; i++) {
9+
const currentNum = nums[i];
10+
const tempMax = Math.max(
11+
currentNum,
12+
prevMax * currentNum,
13+
prevMin * currentNum
14+
);
15+
prevMin = Math.min(currentNum, prevMax * currentNum, prevMin * currentNum);
16+
prevMax = tempMax;
17+
maxProd = Math.max(maxProd, prevMax);
18+
}
19+
20+
return maxProd;
21+
}
22+
23+
24+
// TC: O(n^2)
25+
// SC: O(1)
26+
// function maxProduct(nums: number[]): number {
27+
// let largestProd = -Infinity;
28+
// let subProd = 1;
29+
30+
// for (let i = 0; i < nums.length; i++) {
31+
// subProd = 1;
32+
// for (let j = i; j < nums.length; j++) {
33+
// subProd *= nums[j];
34+
// largestProd = Math.max(largestProd, subProd);
35+
// }
36+
// }
37+
38+
// return largestProd;
39+
// }
40+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// TC: O(m+ n), where m is the length of s, and n is the length of t
2+
// SC: O(n)
3+
4+
function minWindow(s: string, t: string): string {
5+
const tCnt = new Map<string, number>();
6+
const windowCnt = new Map<string, number>();
7+
8+
for (const ch of t) {
9+
tCnt.set(ch, (tCnt.get(ch) || 0) + 1);
10+
}
11+
12+
let left = 0;
13+
let validWindowKeySize = 0;
14+
let minStrLength = Infinity;
15+
let minLeft = 0;
16+
17+
for (let right = 0; right < s.length; right++) {
18+
const ch = s[right];
19+
20+
windowCnt.set(ch, (windowCnt.get(ch) || 0) + 1);
21+
22+
if (tCnt.has(ch) && tCnt.get(ch) === windowCnt.get(ch)) {
23+
validWindowKeySize++;
24+
}
25+
26+
// When windowCnt's keys include all the keys from tCnt
27+
while (tCnt.size === validWindowKeySize && left <= right) {
28+
// Update the minimum window details: start index (minLeft) and length (minStrLength)
29+
if (right - left + 1 < minStrLength) {
30+
minStrLength = right - left + 1;
31+
minLeft = left;
32+
}
33+
34+
// shrink the window by moving the left pointer
35+
const ch = s[left];
36+
windowCnt.set(ch, windowCnt.get(ch)! - 1);
37+
38+
if (tCnt.has(ch) && windowCnt.get(ch)! < tCnt.get(ch)!) {
39+
validWindowKeySize--;
40+
}
41+
42+
left++;
43+
}
44+
}
45+
46+
return minStrLength === Infinity
47+
? ""
48+
: s.slice(minLeft, minLeft + minStrLength);
49+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// TC: O(m * n)
2+
// SC: O(m * n)
3+
4+
function pacificAtlantic(heights: number[][]): number[][] {
5+
const m = heights.length;
6+
const n = heights[0].length;
7+
8+
const pacific: boolean[][] = Array.from({ length: m }, () =>
9+
Array(n).fill(false)
10+
);
11+
const atlantic: boolean[][] = Array.from({ length: m }, () =>
12+
Array(n).fill(false)
13+
);
14+
15+
const directions = [
16+
[0, 1],
17+
[0, -1],
18+
[1, 0],
19+
[-1, 0],
20+
];
21+
22+
const dfs = (row: number, col: number, visited: boolean[][]) => {
23+
visited[row][col] = true;
24+
// Border cells are adjacent to the ocean, so they are marked as reachable (true)
25+
26+
// From each ocean-border cell, we explore inward:
27+
// if an adjacent cell is higher or equal, water can flow from it to the ocean
28+
// → mark it as reachable (true)
29+
for (const [dr, dc] of directions) {
30+
const newRow = row + dr;
31+
const newCol = col + dc;
32+
33+
if (
34+
newRow >= 0 &&
35+
newRow < m &&
36+
newCol >= 0 &&
37+
newCol < n &&
38+
!visited[newRow][newCol] &&
39+
heights[row][col] <= heights[newRow][newCol]
40+
// heights[row][col] <= heights[newRow][newCol]
41+
// → Water can flow from (newRow, newCol) to (row, col),
42+
// so (row, col) is reachable from the ocean through (newRow, newCol)
43+
) {
44+
dfs(newRow, newCol, visited);
45+
}
46+
}
47+
};
48+
49+
for (let i = 0; i < m; i++) {
50+
dfs(i, 0, pacific);
51+
dfs(i, n - 1, atlantic);
52+
}
53+
54+
for (let j = 0; j < n; j++) {
55+
dfs(0, j, pacific);
56+
dfs(m - 1, j, atlantic);
57+
}
58+
59+
let result: number[][] = [];
60+
61+
for (let i = 0; i < m; i++) {
62+
for (let j = 0; j < n; j++) {
63+
if (pacific[i][j] && atlantic[i][j]) {
64+
result.push([i, j]);
65+
}
66+
}
67+
}
68+
69+
return result;
70+
}
71+

sum-of-two-integers/Jeehay28.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// TC: O(1)
2+
// SC: O(1)
3+
function getSum(a: number, b: number): number {
4+
while (b !== 0) {
5+
let carry = a & b;
6+
a = a ^ b; // sum WITHOUT the carry
7+
b = carry << 1; // new carry
8+
}
9+
10+
return a;
11+
}
12+

0 commit comments

Comments
 (0)