Skip to content

Commit 76fe4b5

Browse files
authored
Merge pull request DaleStudy#500 from wogha95/main
[์žฌํ˜ธ] WEEK 08 Solutions
2 parents 3889d5e + 48be0f5 commit 76fe4b5

File tree

5 files changed

+259
-0
lines changed

5 files changed

+259
-0
lines changed

โ€Žclone-graph/wogha95.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* TC: O(V + E)
3+
* ๋ชจ๋“  ์ •์ ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋˜๊ณ 
4+
* ๋ฐฉ๋ฌธํ•œ ์ •์ ์—์„œ ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ์„ queue์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
5+
* ๋”ฐ๋ผ์„œ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ์ •์  + ๊ฐ„์„ ์— ์„ ํ˜•์ ์œผ๋กœ ๋น„๋ก€ํ•ฉ๋‹ˆ๋‹ค.
6+
*
7+
* SC: O(V + E)
8+
* memory, visitedNodeVal ์—์„œ V๋งŒํผ ๋ฐ์ดํ„ฐ ๊ณต๊ฐ„์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
9+
* ๊ทธ๋ฆฌ๊ณ  queue์—์„œ ์ตœ๋Œ€ ๋ชจ๋“  ๊ฐ„์„  ๊ฐฏ์ˆ˜ * 2 ๋งŒํผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
10+
* ๋”ฐ๋ผ์„œ O(V + 2E) = O(V + E)๋กœ ๊ณ„์‚ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
11+
*
12+
* V: vertex, E: edge
13+
*/
14+
15+
/**
16+
* // Definition for a _Node.
17+
* function _Node(val, neighbors) {
18+
* this.val = val === undefined ? 0 : val;
19+
* this.neighbors = neighbors === undefined ? [] : neighbors;
20+
* };
21+
*/
22+
23+
/**
24+
* @param {_Node} node
25+
* @return {_Node}
26+
*/
27+
var cloneGraph = function (node) {
28+
if (!node) {
29+
return null;
30+
}
31+
32+
const memory = new Map();
33+
const visitedNodeVal = new Set();
34+
35+
return bfsClone(node);
36+
37+
// 1. bfs๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ deep cloneํ•œ graph์˜ head๋ฅผ ๋ฐ˜ํ™˜
38+
function bfsClone(start) {
39+
const queue = [start];
40+
const clonedHeadNode = new _Node(start.val);
41+
memory.set(start.val, clonedHeadNode);
42+
43+
while (queue.length > 0) {
44+
const current = queue.shift();
45+
if (visitedNodeVal.has(current.val)) {
46+
continue;
47+
}
48+
49+
const clonedCurrentNode = getCloneNode(current.val);
50+
51+
for (const neighbor of current.neighbors) {
52+
const clonedNeighborNode = getCloneNode(neighbor.val);
53+
clonedCurrentNode.neighbors.push(clonedNeighborNode);
54+
55+
queue.push(neighbor);
56+
}
57+
58+
visitedNodeVal.add(current.val);
59+
}
60+
61+
return clonedHeadNode;
62+
}
63+
64+
// 2. memory์— val์— ํ•ด๋‹นํ•˜๋Š” node ๋ฐ˜ํ™˜ (์—†์„ ๊ฒฝ์šฐ ์ƒ์„ฑ)
65+
function getCloneNode(val) {
66+
if (!memory.has(val)) {
67+
const node = new _Node(val);
68+
memory.set(val, node);
69+
return node;
70+
}
71+
72+
return memory.get(val);
73+
}
74+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* ์•Œ๊ณ ๋‹ฌ๋ ˆ ํ’€์ด ์ฐธ๊ณ ํ•ด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
3+
* @see https://www.algodale.com/problems/longest-common-subsequence/
4+
*
5+
* DP + 2์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•œ ํ’€์ด
6+
* ํ–‰์€ text1, ์—ด์€ text2์— ๋Œ€์‘ํ•˜๊ณ 
7+
* index๋Š” text์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ช‡๊ฐœ ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ• ๊ฑด์ง€ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
8+
* ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๊ฐ€ ๋™์ผํ•˜๋‹ค๋ฉด dp[index1][index2] = dp[index1 - 1][index2 - 1] + 1
9+
* ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด dp[index1][index2] = Math.max(dp[index1 - 1][index2], dp[index1][index2 - 1])
10+
*
11+
* TC: O(T1 * T2)
12+
* 2์ฐจ์› ๋ฐฐ์—ด์„ ์ˆœํšŒ
13+
*
14+
* SC: O(T1 * T2)
15+
* 2์ฐจ์› ๋ฐฐ์—ด ์ƒ์„ฑ
16+
*
17+
* T1: text1.length, T2: text2.length
18+
*/
19+
20+
/**
21+
* @param {string} text1
22+
* @param {string} text2
23+
* @return {number}
24+
*/
25+
var longestCommonSubsequence = function (text1, text2) {
26+
const LENGTH1 = text1.length;
27+
const LENGTH2 = text2.length;
28+
const memory = Array.from({ length: LENGTH1 + 1 }, () =>
29+
Array.from({ length: LENGTH2 + 1 }, () => 0)
30+
);
31+
32+
for (let index1 = 1; index1 <= LENGTH1; index1++) {
33+
for (let index2 = 1; index2 <= LENGTH2; index2++) {
34+
if (text1[index1 - 1] === text2[index2 - 1]) {
35+
memory[index1][index2] = memory[index1 - 1][index2 - 1] + 1;
36+
} else {
37+
memory[index1][index2] = Math.max(
38+
memory[index1 - 1][index2],
39+
memory[index1][index2 - 1]
40+
);
41+
}
42+
}
43+
}
44+
45+
return memory[LENGTH1][LENGTH2];
46+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* ์•Œ๊ณ ๋‹ฌ๋ ˆ ํ’€์ด ์ฐธ๊ณ 
3+
* @see https://www.algodale.com/problems/longest-repeating-character-replacement/
4+
*
5+
* TC: O(S)
6+
* right์˜ ์ˆœํšŒ + left์˜ ์ˆœํšŒ
7+
* (๊ณฑ์ด ์•„๋‹Œ ๋”ํ•˜๊ธฐ์ธ ์ด์œ ๋Š” right ์ˆœํšŒ๋™์•ˆ left ์ˆœํšŒ์˜ ์ตœ๋Œ€ ์ดํ•ฉ์ด S์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.)
8+
*
9+
* SC: O(1)
10+
* ์ตœ์•…์˜ ๊ฒฝ์šฐ 26๊ฐœ์˜ ์†Œ๋ฌธ์ž๋ฅผ ์ €์žฅํ•˜๋Š” memoryMap์œผ๋กœ ์ธํ•ด ์ƒ์ˆ˜ ๋ณต์žก๋„๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
11+
*
12+
* S: s.length
13+
*/
14+
15+
/**
16+
* @param {string} s
17+
* @param {number} k
18+
* @return {number}
19+
*/
20+
var characterReplacement = function (s, k) {
21+
// 1. ๊ฐ€์žฅ ๊ธด subString ๊ธธ์ด
22+
let result = 0;
23+
24+
// 2. left, right ํฌ์ธํ„ฐ ์‚ฌ์ด์—์„œ ๋“ฑ์žฅํ•œ ๋ฌธ์ž ํšŸ์ˆ˜๋ฅผ ๊ธฐ๋กํ•œ Map๊ณผ ์ตœ๋‹ค ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ ๊ธฐ๋กํ•œ ๋ณ€์ˆ˜
25+
const memory = new Map();
26+
let maxCount = 0;
27+
28+
let left = 0;
29+
let right = 0;
30+
31+
while (right < s.length) {
32+
// 3. '์ƒˆ๋กœ์šด ๋ฌธ์ž(s[right])์˜ ๊ฐฏ์ˆ˜ ๊ธฐ๋ก'๊ณผ '์ตœ๋‹ค ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜ ๊ฐฑ์‹ '
33+
const newCount = (memory.has(s[right]) ? memory.get(s[right]) : 0) + 1;
34+
memory.set(s[right], newCount);
35+
maxCount = Math.max(maxCount, newCount);
36+
37+
// 4. k๋งŒํผ ๋ณ€๊ฒฝ๊ฐ€๋Šฅํ•œ subString ๊ธธ์ด๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•ด left ์ด๋™
38+
while (right - left + 1 - maxCount > k) {
39+
const newCount = memory.get(s[left]) - 1;
40+
memory.set(s[left], newCount);
41+
left += 1;
42+
}
43+
44+
// 5. ๊ฐ€์žฅ ๊ธด subString ๊ธธ์ด ๊ฐฑ์‹ , right ์ด๋™
45+
result = Math.max(result, right - left + 1);
46+
right += 1;
47+
}
48+
49+
return result;
50+
};

โ€Žmerge-two-sorted-lists/wogha95.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* TC: O(List1 + List2)
3+
* List1, List2 ์ „์ฒด ์ˆœํšŒ 1๋ฒˆ์”ฉ ํ•ฉ๋‹ˆ๋‹ค.
4+
*
5+
* SC: O(1)
6+
* List1, List2์˜ ๊ธธ์ด์™€ ๋ฌด๊ด€ํ•œ ๊ณ ์ •๋œ ๋ฐ์ดํ„ฐ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (head, pointer ๋ณ€์ˆ˜๋“ค)
7+
*
8+
* List1: list1.length, List2.length;
9+
*/
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* function ListNode(val, next) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.next = (next===undefined ? null : next)
16+
* }
17+
*/
18+
/**
19+
* @param {ListNode} list1
20+
* @param {ListNode} list2
21+
* @return {ListNode}
22+
*/
23+
var mergeTwoLists = function (list1, list2) {
24+
// 1. ๋‘˜ ์ค‘ ํ•˜๋‚˜์˜ list๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๋ฐ˜๋Œ€ํŽธ์˜ list๋ฅผ ๋ฐ˜ํ™˜
25+
if (!list1) {
26+
return list2;
27+
}
28+
if (!list2) {
29+
return list1;
30+
}
31+
32+
// 2. ์ •๋‹ต์„ ๋ฐ˜ํ™˜ํ•  ์‹œ์ž‘์ (head)์™€ list ์ˆœํšŒ์‹œ ํ•„์š”ํ•œ pointer
33+
const head = new ListNode();
34+
let headPointer = head;
35+
let list1Pointer = list1;
36+
let list2Pointer = list2;
37+
38+
// 3. ๋‘ list ๋ชจ๋‘ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ง„ ๊ฒฝ์šฐ
39+
while (list1Pointer && list2Pointer) {
40+
if (list1Pointer.val < list2Pointer.val) {
41+
list1Pointer = connectHeadAndListPointer(list1Pointer);
42+
} else {
43+
list2Pointer = connectHeadAndListPointer(list2Pointer);
44+
}
45+
}
46+
47+
// 4. ํ•œ์ชฝ list์˜ ๋‚จ์€ ๋…ธ๋“œ ์—ฐ๊ฒฐ
48+
while (list1Pointer) {
49+
list1Pointer = connectHeadAndListPointer(list1Pointer);
50+
}
51+
52+
while (list2Pointer) {
53+
list2Pointer = connectHeadAndListPointer(list2Pointer);
54+
}
55+
56+
return head.next;
57+
58+
// 5. head์˜ list๋กœ ์—ฐ๊ฒฐ ํ›„ ๋‹ค์Œ ๋…ธ๋“œ๋กœ pointer ์ด๋™
59+
function connectHeadAndListPointer(listPointer) {
60+
headPointer.next = listPointer;
61+
listPointer = listPointer.next;
62+
headPointer = headPointer.next;
63+
64+
return listPointer;
65+
}
66+
};

โ€Žsum-of-two-integers/wogha95.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* TC: O(Bit)
3+
* ์˜ฌ๋ฆผ ๋น„ํŠธ๊ฐ€ 0์ผ๋•Œ๊นŒ์ง€ while์„ ์‹คํ–‰ํ•˜๊ฒŒ๋œ๋‹ค.
4+
*
5+
* SC: O(log(max(a, b)))
6+
*
7+
* Bit: 2์ง„์ˆ˜ a ์™€ b ์ค‘ ๋น„ํŠธ ๊ธธ์ด๊ฐ€ ๊ธด ๊ฒƒ์˜ ๋น„ํŠธ ๊ธธ์ด
8+
*/
9+
10+
/**
11+
* @param {number} a
12+
* @param {number} b
13+
* @return {number}
14+
*/
15+
var getSum = function (a, b) {
16+
while (b !== 0) {
17+
const carry = (a & b) << 1;
18+
a = a ^ b;
19+
b = carry;
20+
}
21+
22+
return a;
23+
};

0 commit comments

Comments
ย (0)