Skip to content

Commit bc5b926

Browse files
authored
Merge pull request #1879 from hyer0705/main
[hyer0705] WEEK 07 solutions
2 parents 477c2fd + 780e017 commit bc5b926

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function lengthOfLongestSubstring(s: string): number {
2+
let maxLen = 0;
3+
4+
const used = new Set<string>();
5+
let windowStart = 0;
6+
7+
for (let windowEnd = 0; windowEnd < s.length; windowEnd++) {
8+
const currentCh = s[windowEnd];
9+
while (used.has(currentCh)) {
10+
used.delete(s[windowStart]);
11+
windowStart++;
12+
}
13+
14+
used.add(currentCh);
15+
maxLen = Math.max(maxLen, windowEnd - windowStart + 1);
16+
}
17+
18+
return maxLen;
19+
}

number-of-islands/hyer0705.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function numIslands(grid: string[][]): number {
2+
const m = grid.length;
3+
const n = grid[0].length;
4+
5+
const LAND = "1";
6+
const WATER = "0";
7+
8+
const visited: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false));
9+
10+
const isValid = (row: number, col: number): boolean => row >= 0 && row < m && col >= 0 && col < n;
11+
12+
const bfs = (row: number, col: number): void => {
13+
const directions = [
14+
[0, 1],
15+
[0, -1],
16+
[1, 0],
17+
[-1, 0],
18+
];
19+
20+
// [row, col][]
21+
const queue: number[][] = [];
22+
23+
visited[row][col] = true;
24+
queue.push([row, col]);
25+
26+
while (queue.length > 0) {
27+
const [cx, cy] = queue.shift()!;
28+
29+
for (const [dx, dy] of directions) {
30+
const [nx, ny] = [cx + dx, cy + dy];
31+
32+
if (isValid(nx, ny) && !visited[nx][ny] && grid[nx][ny] === LAND) {
33+
visited[nx][ny] = true;
34+
queue.push([nx, ny]);
35+
}
36+
}
37+
}
38+
};
39+
40+
let island = 0;
41+
42+
for (let i = 0; i < m; i++) {
43+
for (let j = 0; j < n; j++) {
44+
if (grid[i][j] === LAND && !visited[i][j]) {
45+
island++;
46+
bfs(i, j);
47+
}
48+
}
49+
}
50+
51+
return island;
52+
}

reverse-linked-list/hyer0705.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
// using iterative
14+
function reverseList(head: ListNode | null): ListNode | null {
15+
if (!head) return null;
16+
17+
let prev: ListNode | null = null;
18+
let current = head;
19+
20+
while (current) {
21+
const temp: ListNode | null = current.next;
22+
current.next = prev;
23+
prev = current;
24+
current = temp;
25+
}
26+
27+
return prev;
28+
}
29+
30+
// using recursive
31+
function reverseList(head: ListNode | null): ListNode | null {
32+
if (!head || !head.next) return head;
33+
34+
const newHead = reverseList(head.next);
35+
36+
head.next.next = head;
37+
head.next = null;
38+
39+
return newHead;
40+
}

set-matrix-zeroes/hyer0705.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// mark first row, first col - 0ms
2+
/**
3+
Do not return anything, modify matrix in-place instead.
4+
*/
5+
function setZeroes(matrix: number[][]): void {
6+
const m = matrix.length;
7+
const n = matrix[0].length;
8+
9+
let isFirstColZero = false;
10+
let isFirstRowZero = false;
11+
for (let i = 0; i < m; i++) {
12+
for (let j = 0; j < n; j++) {
13+
if (matrix[i][j] === 0) {
14+
if (!isFirstRowZero && i === 0) isFirstRowZero = true;
15+
if (!isFirstColZero && j === 0) isFirstColZero = true;
16+
matrix[i][0] = 0;
17+
matrix[0][j] = 0;
18+
}
19+
}
20+
}
21+
22+
for (let i = 1; i < m; i++) {
23+
for (let j = 1; j < n; j++) {
24+
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
25+
matrix[i][j] = 0;
26+
}
27+
}
28+
}
29+
30+
if (isFirstRowZero) {
31+
for (let j = 0; j < n; j++) {
32+
matrix[0][j] = 0;
33+
}
34+
}
35+
if (isFirstColZero) {
36+
for (let i = 0; i < m; i++) {
37+
matrix[i][0] = 0;
38+
}
39+
}
40+
}
41+
42+
// using set - 4ms
43+
/**
44+
Do not return anything, modify matrix in-place instead.
45+
*/
46+
function setZeroes(matrix: number[][]): void {
47+
const m = matrix.length;
48+
const n = matrix[0].length;
49+
50+
// `${row},${col}`
51+
const coordinates = new Set<string>();
52+
53+
for (let i = 0; i < m; i++) {
54+
for (let j = 0; j < n; j++) {
55+
if (matrix[i][j] === 0) {
56+
coordinates.add(`${i},${j}`);
57+
}
58+
}
59+
}
60+
61+
for (const coordinate of coordinates) {
62+
const [x, y] = coordinate.split(",");
63+
for (let j = 0; j < n; j++) {
64+
matrix[x][j] = 0;
65+
}
66+
for (let i = 0; i < m; i++) {
67+
matrix[i][y] = 0;
68+
}
69+
}
70+
}

unique-paths/hyer0705.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function uniquePaths(m: number, n: number): number {
2+
const dp: number[][] = Array.from({ length: m }, () => Array(n).fill(1));
3+
4+
for (let i = 1; i < m; i++) {
5+
for (let j = 1; j < n; j++) {
6+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
7+
}
8+
}
9+
10+
return dp[m - 1][n - 1];
11+
}

0 commit comments

Comments
 (0)