Skip to content

Commit c25a352

Browse files
authored
Merge pull request #493 from Sunjae95/main
[선재] WEEK07 문제풀이
2 parents f239dfb + 10d40b1 commit c25a352

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* hash table + two pointer
5+
*
6+
* n = length of s
7+
* time complexity: O(n)
8+
* space complexity: O(n)
9+
*/
10+
var lengthOfLongestSubstring = function (s) {
11+
const map = new Map();
12+
let answer = 0;
13+
let start = 0;
14+
15+
for (let i = 0; i < s.length; i++) {
16+
if (map.has(s[i])) start = Math.max(map.get(s[i]) + 1, start);
17+
map.set(s[i], i);
18+
answer = Math.max(answer, i - start + 1);
19+
}
20+
21+
return answer;
22+
};

number-of-islands/sunjae95.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* hash table + two pointer
5+
*
6+
* n = length of grid
7+
* k = length of grid[index]
8+
* time complexity: O(n * k)
9+
* space complexity: O(n * k)
10+
*/
11+
var numIslands = function (grid) {
12+
let answer = 0;
13+
const visited = Array.from({ length: grid.length }, (_, i) =>
14+
Array.from({ length: grid[i].length }, () => false)
15+
);
16+
17+
const dfs = (r, c) => {
18+
const dr = [0, 1, 0, -1];
19+
const dc = [1, 0, -1, 0];
20+
21+
for (let i = 0; i < 4; i++) {
22+
const nextR = r + dr[i];
23+
const nextC = c + dc[i];
24+
25+
if (
26+
nextR >= 0 &&
27+
nextR < grid.length &&
28+
nextC >= 0 &&
29+
nextC < grid[r].length &&
30+
grid[nextR][nextC] == 1 &&
31+
!visited[nextR][nextC]
32+
) {
33+
visited[nextR][nextC] = true;
34+
dfs(nextR, nextC);
35+
}
36+
}
37+
};
38+
39+
for (let row = 0; row < grid.length; row++) {
40+
for (let column = 0; column < grid[row].length; column++) {
41+
if (grid[row][column] == 1 && !visited[row][column]) {
42+
visited[row][column] = true;
43+
answer++;
44+
dfs(row, column);
45+
}
46+
}
47+
}
48+
return answer;
49+
};

reverse-linked-list/sunjae95.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* Thinking of stacking nodes like stacks while traveling
5+
*
6+
* time complexity: O(n)
7+
* space complexity: O(n)
8+
*/
9+
10+
var reverseList = function (head) {
11+
let answer = null;
12+
13+
const buildReverseList = (target) => {
14+
if (target === null) return;
15+
16+
const node = new ListNode(target.val, answer);
17+
answer = node;
18+
19+
buildReverseList(target.next);
20+
};
21+
22+
buildReverseList(head);
23+
24+
return answer;
25+
};

set-matrix-zeroes/sunjae95.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* memoization
5+
*
6+
* m: length of matrix
7+
* n: length of matrix[i]
8+
* time complexity: O(m * n)
9+
* space complexity: O(m * n)
10+
*/
11+
var setZeroes = function (matrix) {
12+
const stack = [];
13+
const memo = { row: new Set(), column: new Set() };
14+
15+
const setZero = ({ r, c, isRow, isColumn }) => {
16+
const length = isRow ? matrix.length : matrix[0].length;
17+
18+
for (let i = 0; i < length; i++) {
19+
const row = isRow ? i : r;
20+
const column = isColumn ? i : c;
21+
matrix[row][column] = 0;
22+
}
23+
};
24+
25+
matrix.forEach((row, r) => {
26+
row.forEach((value, c) => {
27+
if (value === 0) stack.push([r, c]);
28+
});
29+
});
30+
31+
while (stack.length) {
32+
const [r, c] = stack.pop();
33+
34+
if (!memo.row.has(r)) {
35+
setZero({ r, c, isColumn: true });
36+
memo.row.add(r);
37+
}
38+
39+
if (!memo.column.has(c)) {
40+
setZero({ r, c, isRow: true });
41+
memo.column.add(c);
42+
}
43+
}
44+
45+
return matrix;
46+
};

unique-paths/sunjae95.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* 1. dfs -> time limited
5+
* 2. dynamic programming
6+
*
7+
* time complexity: O(m * n)
8+
* space complexity: O(m * n)
9+
*/
10+
11+
var uniquePaths = function (m, n) {
12+
// initialize
13+
const dp = Array.from({ length: m }, (_, i) =>
14+
Array.from({ length: n }, (_, j) => (i === 0 || j === 0 ? 1 : 0))
15+
);
16+
17+
for (let i = 1; i < m; i++) {
18+
for (let j = 1; j < n; j++) {
19+
// recurrence relation
20+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
21+
}
22+
}
23+
24+
return dp[m - 1][n - 1];
25+
};

0 commit comments

Comments
 (0)