Skip to content

Commit 7718d7b

Browse files
authored
Merge pull request #491 from HC-kang/main
[강희찬] WEEK 7 Solution
2 parents 07b18e7 + 2a6d8a6 commit 7718d7b

File tree

5 files changed

+299
-0
lines changed

5 files changed

+299
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* https://leetcode.com/problems/longest-substring-without-repeating-characters
3+
* T.C. O(n^2)
4+
* S.C. O(n)
5+
*/
6+
// function lengthOfLongestSubstring(s: string): number {
7+
// let max = 0;
8+
// for (let i = 0; i < s.length; i++) {
9+
// const SET = new Set();
10+
// let count = 0;
11+
// for (let j = i; j < s.length; j++) {
12+
// if (SET.has(s[j])) break;
13+
// SET.add(s[j]);
14+
// count += 1;
15+
// max = Math.max(max, count);
16+
// }
17+
// }
18+
// return max;
19+
// }
20+
21+
/**
22+
* T.C. O(n)
23+
* S.C. O(n)
24+
*/
25+
// function lengthOfLongestSubstring(s: string): number {
26+
// let left = 0;
27+
// let right = 0;
28+
// let max = 0;
29+
// const SET = new Set();
30+
31+
// while (s[right]) {
32+
// if (SET.has(s[right])) {
33+
// SET.delete(s[left]);
34+
// left++;
35+
// } else {
36+
// SET.add(s[right]);
37+
// max = Math.max(SET.size, max);
38+
// right++;
39+
// }
40+
// }
41+
42+
// return max;
43+
// }
44+
45+
/**
46+
* T.C. O(n)
47+
* S.C. O(n)
48+
*/
49+
function lengthOfLongestSubstring(s: string): number {
50+
let left = 0;
51+
let right = 0;
52+
let max = 0;
53+
const MAP = new Map<string, number>();
54+
55+
while (right < s.length) {
56+
if (MAP.has(s[right])) {
57+
left = Math.max(MAP.get(s[right])! + 1, left);
58+
}
59+
MAP.set(s[right], right);
60+
max = Math.max(max, right - left + 1);
61+
right++;
62+
}
63+
64+
return max;
65+
}

number-of-islands/HC-kang.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* https://leetcode.com/problems/number-of-islands
3+
* T.C. O(m*n)
4+
* S.C. O(m*n)
5+
*/
6+
// function numIslands(grid: string[][]): number {
7+
// let count = 0;
8+
// let dir = [[1, 0], [0, 1], [-1, 0], [0, -1]];
9+
10+
// function removeIsland(r: number, c: number) {
11+
// if (r < 0 || r >= grid.length) return;
12+
// if (c < 0 || c >= grid[0].length) return;
13+
// if (grid[r][c] === '0') return;
14+
15+
// grid[r][c] = '0';
16+
// for (let [dr, dc] of dir) {
17+
// removeIsland(r + dr, c + dc);
18+
// }
19+
// return 1;
20+
// }
21+
22+
// for (let r = 0; r < grid.length; r++) {
23+
// for (let c = 0; c < grid[0].length; c++) {
24+
// if (grid[r][c] === '0') continue;
25+
// count++;
26+
// removeIsland(r, c);
27+
// }
28+
// }
29+
30+
// return count;
31+
// }
32+
33+
/**
34+
* T.C. O(m*n)
35+
* S.C. O(m*n)
36+
*/
37+
function numIslands(grid: string[][]): number {
38+
let count = 0;
39+
let dir = [[1, 0], [0, 1], [-1, 0], [0, -1]];
40+
41+
function removeIsland(r: number, c: number) {
42+
const stack = [[r, c]];
43+
44+
while (stack.length) {
45+
const [r, c] = stack.pop()!;
46+
if (r < 0 || r >= grid.length) continue;
47+
if (c < 0 || c >= grid[0].length) continue;
48+
if (grid[r][c] === '0') continue;
49+
50+
grid[r][c] = '0';
51+
for (let [dr, dc] of dir) {
52+
stack.push([r + dr, c + dc]);
53+
}
54+
}
55+
}
56+
57+
for (let r = 0; r < grid.length; r++) {
58+
for (let c = 0; c < grid[0].length; c++) {
59+
if (grid[r][c] === '0') continue;
60+
count++;
61+
removeIsland(r, c);
62+
}
63+
}
64+
65+
return count;
66+
}

reverse-linked-list/HC-kang.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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/reverse-linked-list
12+
* T.C. O(n)
13+
* S.C. O(1)
14+
*/
15+
function reverseList(head: ListNode | null): ListNode | null {
16+
let prev: ListNode | null = null;
17+
let current: ListNode | null = head;
18+
while (current !== null) {
19+
const next = current.next;
20+
current.next = prev;
21+
prev = current;
22+
current = next;
23+
}
24+
return prev;
25+
}

set-matrix-zeroes/HC-kang.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* https://leetcode.com/problems/set-matrix-zeroes
3+
* T.C. O(r * c)
4+
* S.C. O(r + c)
5+
*/
6+
function setZeroes(matrix: number[][]): void {
7+
const r = matrix.length;
8+
const c = matrix[0].length;
9+
10+
const zeroRows = new Set<number>();
11+
const zeroCols = new Set<number>();
12+
13+
for (let i = 0; i < r; i++) {
14+
for (let j = 0; j < c; j++) {
15+
if (matrix[i][j] === 0) {
16+
zeroRows.add(i);
17+
zeroCols.add(j);
18+
}
19+
}
20+
}
21+
22+
for (let i = 0; i < r; i++) {
23+
for (let j = 0; j < c; j++) {
24+
if (zeroRows.has(i) || zeroCols.has(j)) {
25+
matrix[i][j] = 0;
26+
}
27+
}
28+
}
29+
}
30+
31+
/**
32+
* T.C. O(r * c)
33+
* S.C. O(1)
34+
*/
35+
function setZeroes(matrix: number[][]): void {
36+
const r = matrix.length;
37+
const c = matrix[0].length;
38+
39+
let firstRowHasZero = false;
40+
let firstColHasZero = false;
41+
42+
if (matrix[0].some((val) => val === 0)) {
43+
firstRowHasZero = true;
44+
}
45+
46+
if (matrix.some((row) => row[0] === 0)) {
47+
firstColHasZero = true;
48+
}
49+
50+
for (let i = 1; i < r; i++) {
51+
for (let j = 1; j < c; j++) {
52+
if (matrix[i][j] === 0) {
53+
matrix[i][0] = 0;
54+
matrix[0][j] = 0;
55+
}
56+
}
57+
}
58+
59+
for (let i = 1; i < r; i++) {
60+
if (matrix[i][0] === 0) {
61+
matrix[i].fill(0);
62+
}
63+
}
64+
65+
for (let j = 1; j < c; j++) {
66+
if (matrix[0][j] === 0) {
67+
matrix.forEach((row) => (row[j] = 0));
68+
}
69+
}
70+
71+
if (firstRowHasZero) {
72+
matrix[0].fill(0);
73+
}
74+
75+
if (firstColHasZero) {
76+
matrix.forEach((row) => (row[0] = 0));
77+
}
78+
}

unique-paths/HC-kang.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Solution 1. recursive - failed with Stack Overflow
3+
*/
4+
function uniquePaths(m: number, n: number): number {
5+
function factorialMemo() {
6+
const cache = [0, 1];
7+
return function factorial(n: number) {
8+
if (cache[n]) return cache[n];
9+
cache[n] = n * factorial(n - 1);
10+
return cache[n];
11+
};
12+
}
13+
14+
const factorial = factorialMemo();
15+
const total = m + n - 2;
16+
const right = m - 1;
17+
return Math.round(
18+
factorial(total) / (factorial(right) * factorial(total - right))
19+
);
20+
}
21+
22+
/**
23+
* Solution 2. for loop (with some 야매.. but it works)
24+
* https://leetcode.com/problems/unique-paths
25+
* T.C. O(m + n)
26+
* S.C. O(m + n)
27+
*/
28+
function uniquePaths(m: number, n: number): number {
29+
function factorialMemo() {
30+
const cache = [1, 1];
31+
return function factorial(n: number) {
32+
if (cache[n]) return cache[n];
33+
let result = cache[cache.length - 1];
34+
for (let i = cache.length; i <= n; i++) {
35+
result = result * i;
36+
cache[i] = result;
37+
}
38+
return result;
39+
};
40+
}
41+
42+
const factorial = factorialMemo();
43+
const total = m + n - 2;
44+
const right = m - 1;
45+
return Math.round(
46+
factorial(total) / (factorial(right) * factorial(total - right))
47+
);
48+
}
49+
50+
/**
51+
* Solution 3. DP
52+
* T.C. O(m * n)
53+
* S.C. O(m * n)
54+
*/
55+
function uniquePaths(m: number, n: number): number {
56+
const dp: number[][] = Array.from({ length: m }, () => Array(n).fill(1));
57+
58+
for (let i = 1; i < m; i++) {
59+
for (let j = 1; j < n; j++) {
60+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
61+
}
62+
}
63+
64+
return dp[m - 1][n - 1];
65+
}

0 commit comments

Comments
 (0)