Skip to content

Commit f99d21c

Browse files
authored
Merge pull request #502 from HC-kang/main
[강희찬] WEEK 8 Solution
2 parents e0acb77 + f690485 commit f99d21c

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

clone-graph/HC-kang.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* https://leetcode.com/problems/clone-graph
3+
* T.C. O(N)
4+
* S.C. O(N)
5+
*/
6+
function cloneGraph(node: _Node | null): _Node | null {
7+
if (!node) return null;
8+
9+
const map = new Map<number, _Node>();
10+
return dfs(node);
11+
12+
function dfs(node: _Node): _Node {
13+
if (map.has(node.val)) return map.get(node.val)!;
14+
15+
const newNode = new _Node(node.val);
16+
map.set(node.val, newNode);
17+
18+
for (let neighbor of node.neighbors) {
19+
newNode.neighbors.push(dfs(neighbor));
20+
}
21+
22+
return newNode;
23+
}
24+
}
25+
26+
/**
27+
* T.C. O(N)
28+
* S.C. O(N)
29+
*/
30+
function cloneGraph(node: _Node | null): _Node | null {
31+
if (!node) return null;
32+
33+
const map = new Map<number, _Node>();
34+
const stack = [node];
35+
const newNode = new _Node(node.val);
36+
map.set(node.val, newNode);
37+
38+
while (stack.length) {
39+
const node = stack.pop()!;
40+
const newNode = map.get(node.val)!;
41+
42+
for (let neighbor of node.neighbors) {
43+
if (!map.has(neighbor.val)) {
44+
stack.push(neighbor);
45+
const newNeighbor = new _Node(neighbor.val);
46+
map.set(neighbor.val, newNeighbor);
47+
}
48+
newNode.neighbors.push(map.get(neighbor.val)!);
49+
}
50+
}
51+
52+
return newNode;
53+
}
54+
55+
class _Node {
56+
val: number;
57+
neighbors: _Node[];
58+
59+
constructor(val?: number, neighbors?: _Node[]) {
60+
this.val = val === undefined ? 0 : val;
61+
this.neighbors = neighbors === undefined ? [] : neighbors;
62+
}
63+
}

longest-common-subsequence/HC-kang.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* https://leetcode.com/problems/longest-common-subsequence
3+
* T.C. O(m * n)
4+
* S.C. O(n)
5+
*/
6+
function longestCommonSubsequence(text1: string, text2: string): number {
7+
const dp = Array.from({ length: text2.length + 1 }, () => 0);
8+
9+
for (let i = 1; i <= text1.length; i++) {
10+
let prev = 0;
11+
for (let j = 1; j <= text2.length; j++) {
12+
const temp = dp[j];
13+
if (text1[i - 1] === text2[j - 1]) {
14+
dp[j] = prev + 1;
15+
} else {
16+
dp[j] = Math.max(dp[j], dp[j - 1]);
17+
}
18+
prev = temp;
19+
}
20+
}
21+
22+
return dp[text2.length];
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* https://leetcode.com/problems/longest-repeating-character-replacement
3+
* T.C. O(n)
4+
* S.C. O(1)
5+
*/
6+
function characterReplacement(s: string, k: number): number {
7+
const charCount = new Array(26).fill(0);
8+
let maxCount = 0;
9+
let start = 0;
10+
let maxLen = 0;
11+
12+
const A = 'A'.charCodeAt(0);
13+
for (let end = 0; end < s.length; end++) {
14+
const endCharIdx = s.charCodeAt(end) - A;
15+
maxCount = Math.max(maxCount, ++charCount[endCharIdx]);
16+
17+
if (end - start + 1 - maxCount > k) {
18+
charCount[s.charCodeAt(start) - A]--;
19+
start++;
20+
}
21+
22+
maxLen = Math.max(maxLen, end - start + 1);
23+
}
24+
return maxLen;
25+
}

merge-two-sorted-lists/HC-kang.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = val === undefined ? 0 : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}
9+
10+
/**
11+
* https://leetcode.com/problems/merge-two-sorted-lists
12+
* T.C. O(m + n)
13+
* S.C. O(1)
14+
*/
15+
function mergeTwoLists(
16+
list1: ListNode | null,
17+
list2: ListNode | null
18+
): ListNode | null {
19+
let head = new ListNode();
20+
let current = head;
21+
22+
while (list1 && list2) {
23+
if (list1.val < list2.val) {
24+
current.next = list1;
25+
list1 = list1.next;
26+
} else {
27+
current.next = list2;
28+
list2 = list2.next;
29+
}
30+
current = current.next;
31+
}
32+
33+
current.next = list1 || list2;
34+
35+
return head.next;
36+
}

sum-of-two-integers/HC-kang.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* https://leetcode.com/problems/sum-of-two-integers
3+
* T.C. O(log a)
4+
* S.C. O(1)
5+
*/
6+
function getSum(a: number, b: number): number {
7+
while (b != 0) {
8+
let carry = a & b;
9+
a = a ^ b;
10+
b = carry << 1;
11+
}
12+
return a;
13+
}

0 commit comments

Comments
 (0)