Skip to content

Commit ade173c

Browse files
authored
Merge pull request #1508 from Moonjonghoo/main
[moonjonghoo] WEEK 08 solutions
2 parents dfcad35 + 098111e commit ade173c

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

โ€Žclone-graph/moonjonghoo.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* // Definition for a Node.
3+
* function Node(val, neighbors) {
4+
* this.val = val === undefined ? 0 : val;
5+
* this.neighbors = neighbors === undefined ? [] : neighbors;
6+
* };
7+
*/
8+
9+
/**
10+
* @param {Node} node
11+
* @return {Node}
12+
*/
13+
var cloneGraph = function (node) {
14+
if (!node) return null;
15+
16+
const visited = new Map();
17+
18+
const dfs = (currNode) => {
19+
if (visited.has(currNode)) {
20+
return visited.get(currNode);
21+
}
22+
23+
// ๋…ธ๋“œ ๋ณต์‚ฌ
24+
const clone = new Node(currNode.val);
25+
visited.set(currNode, clone);
26+
27+
// ์ด์›ƒ ๋…ธ๋“œ๋“ค๋„ ๋ณต์‚ฌํ•ด์„œ ์—ฐ๊ฒฐ
28+
for (let neighbor of currNode.neighbors) {
29+
clone.neighbors.push(dfs(neighbor));
30+
}
31+
32+
return clone;
33+
};
34+
35+
return dfs(node);
36+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} text1
3+
* @param {string} text2
4+
* @return {number}
5+
*/
6+
var longestCommonSubsequence = function (text1, text2) {
7+
const m = text1.length,
8+
n = text2.length;
9+
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
10+
11+
for (let i = 1; i <= m; i++) {
12+
for (let j = 1; j <= n; j++) {
13+
if (text1[i - 1] === text2[j - 1]) {
14+
dp[i][j] = dp[i - 1][j - 1] + 1;
15+
} else {
16+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
17+
}
18+
}
19+
}
20+
21+
return dp[m][n];
22+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var characterReplacement = function (s, k) {
7+
let left = 0;
8+
let maxCount = 0;
9+
let freq = new Array(26).fill(0); // A~Z
10+
11+
let maxLength = 0;
12+
13+
for (let right = 0; right < s.length; right++) {
14+
const idx = s.charCodeAt(right) - "A".charCodeAt(0);
15+
freq[idx]++;
16+
maxCount = Math.max(maxCount, freq[idx]);
17+
18+
let windowSize = right - left + 1;
19+
20+
if (windowSize - maxCount > k) {
21+
const leftIdx = s.charCodeAt(left) - "A".charCodeAt(0);
22+
freq[leftIdx]--;
23+
left++;
24+
}
25+
26+
maxLength = Math.max(maxLength, right - left + 1);
27+
}
28+
29+
return maxLength;
30+
};

โ€Žnumber-of-islands/moonjonghoo.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {character[][]} grid
3+
* @return {number}
4+
*/
5+
const numIslands = function (grid) {
6+
let count = 0;
7+
8+
const dfs = (i, j) => {
9+
if (
10+
i < 0 ||
11+
i >= grid.length ||
12+
j < 0 ||
13+
j >= grid[i].length ||
14+
grid[i][j] === "0"
15+
) {
16+
return;
17+
}
18+
19+
grid[i][j] = "0";
20+
21+
dfs(i + 1, j);
22+
dfs(i - 1, j);
23+
dfs(i, j + 1);
24+
dfs(i, j - 1);
25+
};
26+
27+
for (let i = 0; i < grid.length; i++) {
28+
for (let j = 0; j < grid[i].length; j++) {
29+
if (grid[i][j] === "1") {
30+
dfs(i, j);
31+
count++;
32+
}
33+
}
34+
}
35+
36+
return count;
37+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var countSubstrings = function (s) {
6+
let count = 0;
7+
8+
const expand = (left, right) => {
9+
while (left >= 0 && right < s.length && s[left] === s[right]) {
10+
count++;
11+
left--;
12+
right++;
13+
}
14+
};
15+
16+
for (let i = 0; i < s.length; i++) {
17+
expand(i, i); // ํ™€์ˆ˜ ๊ธธ์ด ์ค‘์‹ฌ (์˜ˆ: "aba")
18+
expand(i, i + 1); // ์ง์ˆ˜ ๊ธธ์ด ์ค‘์‹ฌ (์˜ˆ: "aa")
19+
}
20+
21+
return count;
22+
};

โ€Žreverse-bits/moonjonghoo.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {number} n - a positive integer
3+
* @return {number} - reversed bits
4+
*/
5+
var reverseBits = function (n) {
6+
let result = 0;
7+
for (let i = 0; i < 32; i++) {
8+
result <<= 1; // ์™ผ์ชฝ์œผ๋กœ 1๋น„ํŠธ ์ด๋™
9+
result |= n & 1; // ๋งˆ์ง€๋ง‰ ๋น„ํŠธ ์ถ”์ถœํ•ด์„œ ๊ฒฐ๊ณผ์— ์ถ”๊ฐ€
10+
n >>>= 1; // ๋ถ€ํ˜ธ ์—†๋Š” ์šฐ์ธก ์‹œํ”„ํŠธ (>>>)
11+
}
12+
return result >>> 0; // unsigned 32๋น„ํŠธ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜
13+
};

0 commit comments

Comments
ย (0)