Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f2040c7

Browse files
authoredMay 16, 2025
Merge pull request #1464 from Jeehay28/main
[Jeehay28] Week 07 Solutions
2 parents f7f7e8d + 3a1c92d commit f2040c7

File tree

5 files changed

+232
-0
lines changed

5 files changed

+232
-0
lines changed
 
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// TC: O(n)
2+
// SC: O(n)
3+
4+
function lengthOfLongestSubstring(s: string): number {
5+
let seen = new Map<string, number>();
6+
let maxLength = 0;
7+
let start = 0;
8+
9+
for (let end = 0; end < s.length; end++) {
10+
const ch = s[end];
11+
12+
if (seen.has(ch) && seen.get(ch)! >= start) {
13+
start = seen.get(ch)! + 1;
14+
}
15+
16+
seen.set(ch, end);
17+
maxLength = Math.max(maxLength, end - start + 1);
18+
}
19+
20+
return maxLength;
21+
}

‎number-of-islands/Jeehay28.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// TC: O(n), where N = total number of cells = m * n
2+
// SC: O(n)
3+
4+
function numIslands(grid: string[][]): number {
5+
const sink = (row: number, col: number) => {
6+
visited[row][col] = true;
7+
8+
const dirs = [
9+
[row - 1, col],
10+
[row + 1, col],
11+
[row, col - 1],
12+
[row, col + 1],
13+
];
14+
15+
for (const dir of dirs) {
16+
const [r, c] = dir;
17+
18+
if (r >= 0 && r < grid.length && c >= 0 && c < grid[0].length) {
19+
if (!visited[r][c] && grid[r][c] === "1") {
20+
sink(r, c);
21+
}
22+
}
23+
}
24+
};
25+
26+
let count = 0;
27+
const visited: boolean[][] = Array.from({ length: grid.length }, () =>
28+
Array(grid[0].length).fill(false)
29+
);
30+
31+
for (let i = 0; i < grid.length; i++) {
32+
for (let j = 0; j < grid[0].length; j++) {
33+
if (!visited[i][j] && grid[i][j] === "1") {
34+
count++;
35+
sink(i, j); // // Sink all connected neighboring land cells
36+
}
37+
}
38+
}
39+
40+
return count;
41+
}

‎reverse-linked-list/Jeehay28.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 reverseList(head: ListNode | null): ListNode | null {
13+
if (!head) return head;
14+
15+
// 1 -> 2 -> 3 -> 4 -> 5 -> null
16+
17+
// null <- 1
18+
// prev curr
19+
20+
// null <- 1 <- 2
21+
// prev cur
22+
23+
let prev: ListNode | null = null;
24+
let curr: ListNode | null = head;
25+
26+
while (curr) {
27+
const tempNext: ListNode | null = curr.next; // 2 -> 3 -> 4 -> 5 -> null
28+
curr.next = prev; // curr: 1 -> null
29+
prev = curr; // curr: 1 -> null, 2 -> 1 -> null
30+
curr = tempNext;
31+
}
32+
33+
return prev;
34+
}

‎set-matrix-zeroes/Jeehay28.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
Do not return anything, modify matrix in-place instead.
3+
*/
4+
5+
// TC: O(m * n)
6+
// ✅ SC: O(1)
7+
function setZeroes(matrix: number[][]): void {
8+
let isFirstRowZero: boolean = false;
9+
let isFirstColZero: boolean = false;
10+
11+
// check if the first row has any zeros
12+
for (let col = 0; col < matrix[0].length; col++) {
13+
if (matrix[0][col] === 0) {
14+
isFirstRowZero = true;
15+
break;
16+
}
17+
}
18+
19+
// check if the first column has any zeros
20+
for (let row = 0; row < matrix.length; row++) {
21+
if (matrix[row][0] === 0) {
22+
isFirstColZero = true;
23+
break;
24+
}
25+
}
26+
27+
// Use the first row and column to mark rows and columns that need to be zeroed
28+
for (let row = 1; row < matrix.length; row++) {
29+
for (let col = 1; col < matrix[0].length; col++) {
30+
if (matrix[row][col] === 0) {
31+
matrix[row][0] = 0;
32+
matrix[0][col] = 0;
33+
}
34+
}
35+
}
36+
37+
// Set matrix cells to zero based on markers in the first row and column
38+
for (let row = 1; row < matrix.length; row++) {
39+
for (let col = 1; col < matrix[0].length; col++) {
40+
if (matrix[row][0] === 0 || matrix[0][col] === 0) {
41+
matrix[row][col] = 0;
42+
}
43+
}
44+
}
45+
46+
// Zero out the first row if needed
47+
if (isFirstRowZero) {
48+
for (let col = 0; col < matrix[0].length; col++) {
49+
matrix[0][col] = 0;
50+
}
51+
}
52+
53+
// Zero out the first column if needed
54+
if (isFirstColZero) {
55+
for (let row = 0; row < matrix.length; row++) {
56+
matrix[row][0] = 0;
57+
}
58+
}
59+
}
60+
61+
62+
// TC: O(m * n)
63+
// SC: O(m + n)
64+
/*
65+
function setZeroes(matrix: number[][]): void {
66+
67+
const rows = new Set<number>();
68+
const cols = new Set<number>();
69+
70+
// Identify all rows and columns that contain at least one zero
71+
for (let row = 0; row < matrix.length; row++) {
72+
for (let col = 0; col < matrix[0].length; col++) {
73+
if (matrix[row][col] === 0) {
74+
rows.add(row);
75+
cols.add(col);
76+
}
77+
}
78+
}
79+
80+
// Set all elements in the identified rows to zero
81+
for (const row of rows) {
82+
for (let col = 0; col < matrix[0].length; col++) {
83+
matrix[row][col] = 0;
84+
}
85+
}
86+
87+
// Set all elements in the identified columns to zero
88+
for (const col of cols) {
89+
for (let row = 0; row < matrix.length; row++) {
90+
matrix[row][col] = 0;
91+
}
92+
}
93+
};
94+
*/
95+

‎unique-paths/Jeehay28.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Memoized DFS
2+
// TC: O(m * n)
3+
// SC: O(m * n)
4+
function uniquePaths(m: number, n: number): number {
5+
const memo = new Map<string, number>();
6+
7+
const traverse = (row: number, col: number) => {
8+
if (row >= m || col >= n) return 0;
9+
if (row === m - 1 && col === n - 1) return 1;
10+
const key = `${row}-${col}`;
11+
if (memo.has(key)) return memo.get(key);
12+
13+
const result = traverse(row + 1, col) + traverse(row, col + 1);
14+
memo.set(key, result);
15+
return result;
16+
};
17+
18+
return traverse(0, 0);
19+
}
20+
21+
// DP
22+
// TC: O(m * n)
23+
// SC: O(m * n)
24+
/*
25+
function uniquePaths(m: number, n: number): number {
26+
27+
// 1, 1, 1
28+
// 1, 1+1=2, 1+(1+1)=3
29+
30+
const dp = Array.from({length: m}, () => Array(n).fill(1));
31+
32+
for(let i=1; i<m; i++) {
33+
for(let j=1; j<n; j++) {
34+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
35+
}
36+
}
37+
38+
return dp[m-1][n-1];
39+
};
40+
*/
41+

0 commit comments

Comments
 (0)
Please sign in to comment.