Skip to content

Commit 93acc92

Browse files
authored
Merge pull request #605 from HC-kang/main
[강희찬] WEEK 15 Solution
2 parents d33ff58 + fbbd681 commit 93acc92

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

alien-dictionary/HC-kang.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* https://leetcode.com/problems/alien-dictionary
3+
* T.C. O(N * M) N: number of words, M: average length of word
4+
* S.C. O(1) 26 characters
5+
*/
6+
function alienOrder(words: string[]): string {
7+
const graph: Record<string, Set<string>> = {};
8+
const inDegree: Record<string, number> = {};
9+
10+
// Initialize the graph
11+
for (const word of words) {
12+
for (const char of word) {
13+
if (!graph[char]) {
14+
graph[char] = new Set();
15+
inDegree[char] = 0;
16+
}
17+
}
18+
}
19+
20+
// Build the graph
21+
for (let i = 0; i < words.length - 1; i++) {
22+
const word1 = words[i];
23+
const word2 = words[i + 1];
24+
25+
// Check invalid case: if word1 is longer and is prefix of word2
26+
if (word1.length > word2.length && word1.startsWith(word2)) {
27+
return '';
28+
}
29+
30+
let j = 0;
31+
while (j < Math.min(word1.length, word2.length)) {
32+
if (word1[j] !== word2[j]) {
33+
const curSet = graph[word1[j]];
34+
if (!curSet.has(word2[j])) {
35+
curSet.add(word2[j]);
36+
inDegree[word2[j]]++;
37+
}
38+
break;
39+
}
40+
j++;
41+
}
42+
}
43+
44+
// Topological sort
45+
const queue: string[] = [];
46+
for (const [char, degree] of Object.entries(inDegree)) {
47+
if (degree === 0) {
48+
queue.push(char);
49+
}
50+
}
51+
52+
const result: string[] = [];
53+
while (queue.length) {
54+
const char = queue.shift();
55+
result.push(char!);
56+
for (const next of graph[char!]) {
57+
inDegree[next]--;
58+
if (inDegree[next] === 0) {
59+
queue.push(next);
60+
}
61+
}
62+
}
63+
64+
return result.length === Object.keys(inDegree).length //
65+
? result.join('')
66+
: '';
67+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* https://leetcode.com/problems/longest-palindromic-substring
3+
* T.C. O(n^2)
4+
* S.C. O(1)
5+
*/
6+
function longestPalindrome(s: string): string {
7+
if (s.length < 2) return s;
8+
let start = 0;
9+
let end = 0;
10+
11+
for (let i = 0; i < s.length; i++) {
12+
const len1 = expandBothSides(s, i, i); // odd
13+
const len2 = expandBothSides(s, i, i + 1); // even
14+
const len = Math.max(len1, len2);
15+
16+
if (len > end - start) {
17+
start = i - Math.floor((len - 1) / 2);
18+
end = i + Math.floor(len / 2);
19+
}
20+
}
21+
22+
return s.slice(start, end + 1);
23+
}
24+
25+
function expandBothSides(s: string, left: number, right: number): number {
26+
while (0 <= left && right < s.length && s[left] === s[right]) {
27+
left--;
28+
right++;
29+
}
30+
31+
return right - left - 1;
32+
}

rotate-image/HC-kang.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* https://leetcode.com/problems/rotate-image
3+
* T.C. O(n^2)
4+
* S.C. O(1)
5+
*/
6+
function rotate(matrix: number[][]): void {
7+
const n = matrix.length;
8+
9+
// transpose
10+
for (let i = 0; i < n; i++) {
11+
for (let j = i; j < n; j++) {
12+
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
13+
}
14+
}
15+
16+
// reverse
17+
for (let i = 0; i < n; i++) {
18+
for (let j = 0; j < n / 2; j++) {
19+
[matrix[i][j], matrix[i][n - j - 1]] = [matrix[i][n - j - 1], matrix[i][j]];
20+
}
21+
}
22+
}

subtree-of-another-tree/HC-kang.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// class TreeNode {
2+
// val: number;
3+
// left: TreeNode | null;
4+
// right: TreeNode | null;
5+
// constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
// this.val = val === undefined ? 0 : val;
7+
// this.left = left === undefined ? null : left;
8+
// this.right = right === undefined ? null : right;
9+
// }
10+
// }
11+
12+
/**
13+
* https://leetcode.com/problems/subtree-of-another-tree
14+
* T.C. O(n * m)
15+
* S.C. O(n)
16+
*/
17+
function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean {
18+
if (!root) return false;
19+
if (isSameTree(root, subRoot)) return true;
20+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
21+
}
22+
23+
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
24+
if (!p && !q) return true;
25+
if (!p || !q) return false;
26+
if (p.val !== q.val) return false;
27+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
28+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// class TreeNode {
2+
// val: number;
3+
// left: TreeNode | null;
4+
// right: TreeNode | null;
5+
// constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
// this.val = val === undefined ? 0 : val;
7+
// this.left = left === undefined ? null : left;
8+
// this.right = right === undefined ? null : right;
9+
// }
10+
// }
11+
12+
/**
13+
* https://leetcode.com/problems/validate-binary-search-tree
14+
* T.C. O(n)
15+
* S.C. O(n)
16+
*/
17+
function isValidBST(root: TreeNode | null): boolean {
18+
return validate(root, null, null);
19+
}
20+
21+
function validate(node: TreeNode | null, min: number | null, max: number | null): boolean {
22+
if (!node) return true;
23+
if (min !== null && node.val <= min) return false;
24+
if (max !== null && node.val >= max) return false;
25+
return validate(node.left, min, node.val) && validate(node.right, node.val, max);
26+
}

0 commit comments

Comments
 (0)