Skip to content

Commit d8a0117

Browse files
committed
Solutions week 07
1 parent 7c5283e commit d8a0117

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
abc a l = 3
3+
bca b l = 3
4+
cab c l = 3
5+
abc b l = 1
6+
cb
7+
8+
pw w l = 2
9+
wke w l = 3
10+
w
11+
*/
12+
/*
13+
time complexity : O(n)
14+
space complexity : O(n)
15+
*/
16+
function lengthOfLongestSubstring(s: string): number {
17+
if (s.length === 0) return 0
18+
19+
let maxLen = 0
20+
let start = 0
21+
const charMap = new Map<string, number>()
22+
23+
for (let end = 0; end < s.length; end++) {
24+
const currChar = s[end]
25+
26+
if (charMap.has(currChar) && charMap.get(currChar)! >= start) {
27+
start = charMap.get(currChar)! + 1
28+
}
29+
charMap.set(currChar, end)
30+
maxLen = Math.max(maxLen, end - start + 1)
31+
}
32+
return maxLen
33+
};

number-of-islands/hoyeongkwak.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
time complexity : O(m * n)
3+
space complexity : O(m)
4+
*/
5+
function numIslands(grid: string[][]): number {
6+
const rows = grid.length
7+
const cols = grid[0].length
8+
const direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]
9+
let islands = 0
10+
for (let r = 0; r < rows; r++) {
11+
for (let c = 0; c < cols; c++) {
12+
if (grid[r][c] === '1') {
13+
islands += 1
14+
const queue = new Array()
15+
16+
grid[r][c] = '0'
17+
queue.push([r, c])
18+
while (queue.length > 0) {
19+
let curr = queue.shift()
20+
for (const dir of direction) {
21+
let dr = curr[0] + dir[0]
22+
let dc = curr[1] + dir[1]
23+
if (dr < 0 || dr >= rows || dc < 0 || dc >= cols || grid[dr][dc] === '0') continue
24+
grid[dr][dc] = '0'
25+
queue.push([dr, dc])
26+
}
27+
}
28+
}
29+
}
30+
}
31+
return islands
32+
};

reverse-linked-list/hoyeongkwak.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
/*
14+
curr prev
15+
1, 2, 3, 4, 5 null
16+
2, 3, 4, 5 1
17+
3, 4, 5 2, 1
18+
4, 5 3, 2, 1
19+
5 4, 3, 2, 1
20+
null 5, 4, 3, 2, 1
21+
*/
22+
/*
23+
time complexity : O(n)
24+
space complexity : O(1)
25+
*/
26+
function reverseList(head: ListNode | null): ListNode | null {
27+
let curr = head
28+
let prev = null
29+
while (curr) {
30+
let next = curr.next
31+
curr.next = prev
32+
prev = curr
33+
curr = next
34+
}
35+
return prev
36+
};

set-matrix-zeroes/hoyeongkwak.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
(0, 0) 0~m, 0~n
3+
(1, 1) (0~m, 1) (1, 0~n)
4+
5+
time complexity : O(m * n)
6+
space complexity : O(m * n)
7+
*/
8+
function setZeroes(matrix: number[][]): void {
9+
const rows = matrix.length
10+
const cols = matrix[0].length
11+
const zeroPosition = []
12+
for (let r = 0; r < rows; r++) {
13+
for (let c = 0; c < cols; c++) {
14+
if (matrix[r][c] === 0) {
15+
zeroPosition.push([r,c])
16+
}
17+
}
18+
}
19+
20+
for (let m = 0; m < zeroPosition.length; m++) {
21+
const r = zeroPosition[m][0]
22+
const c = zeroPosition[m][1]
23+
matrix[r].fill(0)
24+
for (let i = 0; i < rows; i++) {
25+
matrix[i][c] = 0
26+
}
27+
}
28+
};

unique-paths/hoyeongkwak.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
time complexity : O(m * n)
3+
space complexity : O(m * n)
4+
*/
5+
function uniquePaths(m: number, n: number): number {
6+
const dp = Array.from({ length: m }, () => Array(n).fill(1))
7+
let results = 0
8+
for (let r = 1; r < m; r++) {
9+
for (let c = 1; c < n; c++) {
10+
dp[r][c] = dp[r - 1][c] + dp[r][c - 1]
11+
}
12+
}
13+
return dp[m-1][n-1]
14+
};

0 commit comments

Comments
 (0)