Skip to content

Commit a51e96e

Browse files
authored
Merge pull request #919 from jdy8739/main
[jdy8739] Week 7
2 parents d1c56fc + edb6386 commit a51e96e

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function(s) {
6+
let start = 0;
7+
let end = 0;
8+
9+
const set = new Set();
10+
11+
let max = 0;
12+
13+
while (end < s.length) {
14+
const char = s[end];
15+
16+
if (set.has(char)) {
17+
set.delete(s[start]);
18+
19+
start++;
20+
} else {
21+
set.add(char);
22+
23+
end++;
24+
}
25+
26+
max = Math.max(max, set.size);
27+
}
28+
29+
return max;
30+
};
31+
32+
// 시간복잡도 O(2n) -> 최대 2n번 반복
33+
// 공간복잡도 O(n) -> 최대 s의 길이만큼 공간을 사용

number-of-islands/jdy8739.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {character[][]} grid
3+
* @return {number}
4+
*/
5+
var numIslands = function (grid) {
6+
const sink = (row, col) => {
7+
grid[row][col] = '0';
8+
9+
const neighbor = [
10+
[row + 1, col], [row, col + 1], [row - 1, col], [row, col - 1]
11+
].filter(([y, x]) => {
12+
return y >= 0 && x >= 0 && y < grid.length && x < grid[0].length;
13+
}).filter(([y, x]) => {
14+
return grid[y][x] === '1';
15+
});
16+
17+
neighbor.forEach(([y, x]) => {
18+
const el = grid[y][x];
19+
20+
if (el === '1') {
21+
sink(y, x);
22+
}
23+
})
24+
}
25+
26+
let count = 0;
27+
28+
for (let i = 0; i < grid.length; i++) {
29+
for (let j = 0; j < grid[0].length; j++) {
30+
31+
if (grid[i][j] === '1') {
32+
count++;
33+
sink(i, j);
34+
}
35+
}
36+
}
37+
38+
return count;
39+
};
40+
41+
// 시간복잡도 O(2 * m * n) -> m * n 만큼 반복 + 재귀적으로 방문할 수 있는 셀의 수는 총 m * n 개
42+
// 공간복잡도 O(1) -> 입력 배열을 사용하지 않고 변수만 사용
43+

reverse-linked-list/jdy8739.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
/**
3+
* Definition for singly-linked list.
4+
* function ListNode(val, next) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.next = (next===undefined ? null : next)
7+
* }
8+
*/
9+
/**
10+
* @param {ListNode} head
11+
* @return {ListNode}
12+
*/
13+
var reverseList = function(head) {
14+
if (head === null) {
15+
return null;
16+
}
17+
18+
if (head.next === null) {
19+
return head;
20+
}
21+
22+
const stack = [];
23+
24+
let nextNode = head;
25+
26+
while (nextNode) {
27+
stack.push(nextNode);
28+
29+
nextNode = nextNode.next;
30+
}
31+
32+
for (let i=stack.length - 1; i>=0; i--) {
33+
if (i === 0) {
34+
stack[i].next = null;
35+
} else {
36+
stack[i].next = stack[i - 1];
37+
}
38+
}
39+
40+
return stack[stack.length - 1];
41+
};
42+
43+
// 시간복잡도 O(2n)
44+
45+

set-matrix-zeroes/jdy8739.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
var setZeroes = function (matrix) {
6+
const coord = [];
7+
8+
for (let i = 0; i < matrix.length; i++) {
9+
for (let j = 0; j < matrix[i].length; j++) {
10+
11+
const num = matrix[i][j];
12+
13+
if (num === 0) {
14+
coord.push({ y: i, x: j });
15+
}
16+
}
17+
}
18+
19+
for (let k = 0; k < coord.length; k++) {
20+
const { y } = coord[k];
21+
22+
for (let j = 0; j < matrix[0].length; j++) {
23+
matrix[y][j] = 0;
24+
}
25+
}
26+
27+
for (let l = 0; l < coord.length; l++) {
28+
const { x } = coord[l];
29+
30+
for (let j = 0; j < matrix.length; j++) {
31+
matrix[j][x] = 0;
32+
}
33+
}
34+
};
35+
36+
// 시간복잡도 O(n * m)
37+
// 공간복잡도 O(n * m)
38+

unique-paths/jdy8739.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var uniquePaths = function (m, n) {
2+
const cache = new Map();
3+
4+
const dfs = (row, col) => {
5+
const cacheKey = `${row}-${col}`;
6+
7+
if (cache.has(cacheKey)) {
8+
return cache.get(cacheKey);
9+
}
10+
11+
if (row === m - 1 && col === n - 1) {
12+
return 1;
13+
}
14+
15+
let count = 0;
16+
17+
if (row < m - 1) {
18+
count += dfs(row + 1, col);
19+
}
20+
21+
if (col < n - 1) {
22+
count += dfs(row, col + 1);
23+
}
24+
25+
cache.set(cacheKey, count);
26+
27+
return count;
28+
}
29+
30+
return dfs(0, 0);
31+
};
32+
33+
// 시간복잡도 O(m * n)
34+
// 공간복잡도 O(m * n) - 1 (matrix[m][n]에 대한 캐시는 포함되지 않으므로)
35+
36+
var uniquePaths = function(m, n) {
37+
const matrix = [];
38+
39+
for (let i=0; i<m; i++) {
40+
const row = new Array(n).fill(1);
41+
matrix.push(row);
42+
}
43+
44+
for (let j=1; j<matrix.length; j++) {
45+
for (let k=1; k<matrix[0].length; k++) {
46+
matrix[j][k] = matrix[j - 1][k] + matrix[j][k - 1];
47+
}
48+
}
49+
50+
return matrix[m - 1][n - 1];
51+
};
52+
53+
// 시간복잡도 O(m * n)
54+
// 공간복잡도 O(m * n)
55+
56+
var uniquePaths = function(m, n) {
57+
const row = new Array(n).fill(1);
58+
59+
for (let i=1; i<m; i++) {
60+
let left = 1;
61+
62+
for (let j=1; j<n; j++) {
63+
row[j] += left;
64+
left = row[j];
65+
}
66+
}
67+
68+
return row[n - 1];
69+
};
70+
71+
// 시간복잡도 O(m * n)
72+
// 공간복잡도 (n)

0 commit comments

Comments
 (0)