Skip to content

Commit a747d1f

Browse files
authored
Merge pull request #955 from jdy8739/main
[jdy8739] week 8
2 parents 111feeb + 550375d commit a747d1f

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed

โ€Žclone-graph/jdy8739.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
const nodeMap = new Map();
15+
16+
const dfs = (nodeParam) => {
17+
if (!nodeParam) {
18+
return null;
19+
}
20+
21+
if (nodeMap.has(nodeParam.val)) {
22+
return nodeMap.get(nodeParam.val);
23+
}
24+
25+
const clone = new Node(nodeParam.val);
26+
27+
nodeMap.set(clone.val, clone);
28+
29+
for (const nei of nodeParam.neighbors) {
30+
clone.neighbors = [...clone.neighbors, dfs(nei)];
31+
}
32+
33+
return clone;
34+
}
35+
36+
return dfs(node);
37+
};
38+
39+
// ์žฌ๊ท€๋ฅผ ํ™œ์šฉํ•œ dfs๋ฅผ ์‚ฌ์šฉํ•œ ํ’€์ด
40+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n + e) -> n(๋…ธ๋“œ์˜ ์ˆ˜) + e(๊ฐ„์„ ์˜ ์ˆ˜) ๋งŒํผ ํƒ์ƒ‰์„ ์ˆ˜ํ–‰
41+
// ๊ณต๊ฐ„๋ณต์žก๋„ O(n) -> map์ด ์ด ๋…ธ๋“œ์˜ ์ˆ˜ ๋งŒํผ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง
42+
43+
// ------------------------------------
44+
45+
/**
46+
* @param {_Node} node
47+
* @return {_Node}
48+
*/
49+
var cloneGraph = function(node) {
50+
if (!node) {
51+
return null;
52+
}
53+
54+
const clone = new Node(node.val);
55+
56+
const clonedNodeMap = new Map().set(clone.val, clone);
57+
58+
// ์ด queue์—๋Š” ํด๋ก ์ด ์•„๋‹ˆ๋ผ ์›๋ณธ ๋…ธ๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€์•ผํ•จ(neighbors์— ๋Œ€ํ•œ ์ฐธ์กฐ ๋•Œ๋ฌธ)
59+
const queue = [node];
60+
61+
while (queue.length > 0) {
62+
const currentNode = queue.pop();
63+
64+
const currentNodeClone = clonedNodeMap.get(currentNode.val);
65+
66+
for (const nei of currentNode.neighbors) {
67+
if (!clonedNodeMap.has(nei.val)) {
68+
clonedNodeMap.set(nei.val, new Node(nei.val));
69+
queue.push(nei);
70+
}
71+
72+
currentNodeClone.neighbors = [...currentNodeClone.neighbors, clonedNodeMap.get(nei.val)];
73+
}
74+
}
75+
76+
return clone;
77+
};
78+
79+
// ํ๋ฅผ ํ™œ์šฉํ•œ bfs๋ฅผ ์‚ฌ์šฉํ•œ ํ’€์ด
80+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n + e) -> n(๋…ธ๋“œ์˜ ์ˆ˜) + e(๊ฐ„์„ ์˜ ์ˆ˜) ๋งŒํผ ํƒ์ƒ‰์„ ์ˆ˜ํ–‰
81+
// ๊ณต๊ฐ„๋ณต์žก๋„ O(n) -> map์ด ์ด ๋…ธ๋“œ์˜ ์ˆ˜ ๋งŒํผ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง
82+
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {string} text1
3+
* @param {string} text2
4+
* @return {number}
5+
*/
6+
var longestCommonSubsequence = function(text1, text2) {
7+
const resultMap = new Map();
8+
9+
const dfs = (i, j) => {
10+
const mapKey = `${i}-${j}`;
11+
12+
if (resultMap.has(mapKey)) {
13+
return resultMap.get(mapKey);
14+
}
15+
16+
if (i === text1.length || j === text2.length) {
17+
return 0;
18+
}
19+
20+
if (text1[i] === text2[j]) {
21+
const resultHit = 1 + dfs(i + 1, j + 1);
22+
resultMap.set(mapKey, resultHit);
23+
return resultHit;
24+
}
25+
26+
const resultMiss = Math.max(dfs(i + 1, j), dfs(i, j + 1));
27+
resultMap.set(mapKey, resultMiss);
28+
return resultMiss;
29+
}
30+
31+
return dfs(0, 0);
32+
};
33+
34+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(m * n) -> ์žฌ๊ท€๋ฅผ ํ†ตํ•ด m๊ณผ n์ด 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ์ƒ๋Œ€ํŽธ์˜ m or n ๋ฒˆ ์งธ ์ธ๋ฑ์Šค ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•˜๋ฏ€๋กœ??? (์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋ทฐ์–ด๋ถ„์ด ์—ฌ์œ ๋˜์‹œ๋ฉด ์„ค๋ช…๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค ใ…œใ…œ)
35+
// ๊ณต๊ฐ„๋ณต์žก๋„ O(m * n) -> ๋งต์— ํ‚ค๊ฐ€ m * n๊ฐœ๋กœ ์ตœ๋Œ€๋กœ ์š”์†Œ๊ฐ€ ๋“ค์–ด์˜ด
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var characterReplacement = function(s, k) {
7+
let start = 0;
8+
let end = 0;
9+
let maxLength = 0;
10+
let maxCountOfChar = 0;
11+
12+
const charMap = new Map();
13+
charMap.set(s[end], 1);
14+
15+
while (end < s.length) {
16+
maxCountOfChar = Math.max(maxCountOfChar, charMap.get(s[end]));
17+
18+
const currLength = end - start + 1;
19+
20+
const countOfOthers = currLength - maxCountOfChar;
21+
22+
// ํ˜„์žฌ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ๋‚˜์˜ค๋Š” ๋ฌธ์ž์—ด์˜ ์ˆ˜๋ฅผ ๋บธ ๊ฐ’์ธ countOfOthers๊ฐ€
23+
// k๋ณด๋‹ค ์ž‘์œผ๋ฉด ํ˜„์žฌ ๋ฌธ์ž์—ด์—์„œ start๋ฒˆ ์งธ ๋ฌธ์ž์˜ ์ˆ˜๋ฅผ map์—์„œ ๊ฐ์†Œ์‹œํ‚ค๊ณ  star์˜ ๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
24+
if (countOfOthers > k) {
25+
const startCharCount = charMap.get(s[start]);
26+
charMap.set(s[start], startCharCount - 1);
27+
28+
start++;
29+
} else {
30+
// countOfOthers๊ฐ€ k๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์„ ๊ฒฝ์šฐ k๋ฒˆ ๋ฌธ์ž๋ฅผ ๋ฐ”๊พธ๋Š” ๊ฒƒ์œผ๋กœ ํ˜„์žฌ ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ํ•˜๋‚˜์˜ ๋ฌธ์ž๋กœ ํ†ต์ผ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์œผ๋กœ
31+
// ํ˜„์žฌ ๋ฌธ์ž์—ด์—์„œ end๋ฒˆ ์งธ ๋ฌธ์ž์˜ ์ˆ˜๋ฅผ map์—์„œ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
32+
// ์ดํ›„ end์˜ ๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
33+
end++;
34+
35+
const endCharCount = charMap.get(s[end]);
36+
37+
if (endCharCount) {
38+
charMap.set(s[end], endCharCount + 1);
39+
} else {
40+
charMap.set(s[end], 1);
41+
}
42+
}
43+
44+
maxLength = Math.max(maxLength, Math.min(maxCountOfChar + k, currLength));
45+
}
46+
47+
return maxLength;
48+
};
49+
50+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n) -> ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๊ธฐ๋ฒ•์œผ๋กœ ์ตœ๋Œ€ 1๋ฒˆ ์ˆœํšŒํ•œ๋‹ค.
51+
// ๊ณต๊ฐ„๋ณต์žก๋„ O(1) -> map์˜ ํฌ๊ธฐ๋Š” ์•ŒํŒŒ๋ฒณ์˜ ๊ฐฏ์ˆ˜์ธ ์ตœ๋Œ€ 26๊ฐœ์ด๋‹ค.

โ€Žnumber-of-1-bits/jdy8739.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var hammingWeight = function (n) {
6+
const binary = [1];
7+
8+
while (binary[0] < n) {
9+
const latest = binary[0] * 2;
10+
11+
binary.unshift(latest);
12+
}
13+
14+
let count = 0;
15+
16+
for (let i = 0; i < binary.length; i++) {
17+
if (binary[i] <= n) {
18+
count++;
19+
n = n - binary[i];
20+
}
21+
22+
if (n === 0) {
23+
break;
24+
}
25+
}
26+
27+
return count;
28+
};
29+
30+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(logn) -> ์ด์ง„ํƒ์ƒ‰์ฒ˜๋Ÿผ 2์”ฉ ๊ณฑํ•ด๊ฐ€๋ฉฐ n์„ ๋„˜์–ด๊ฐ€๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์ฐพ์œผ๋ฏ€๋กœ
31+
32+
// ์ˆ˜์ •๋œ ์‹œ๊ฐ„๋ณต์žก๋„ -> ์œ„์˜ while ๋ฌธ์—์„œ O(logn)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š” unshift ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ
33+
// for๋ฌธ์—์„œ ๋‹ค์‹œ O(logn)์˜ ์‹œ๊ฐ„๋ณต์žก๋„์˜ ๋ฃจํ”„๋ฅผ ๋Œ๊ธฐ ๋•Œ๋ฌธ์—
34+
// ์ด ๋ณต์žก๋„๋Š” O(logn) * binary ๋ฐฐ์—ด์˜ ๊ธธ์ด + O(logn)

0 commit comments

Comments
ย (0)