Skip to content

Commit 2f5427e

Browse files
authored
Merge pull request #1962 from hyer0705/main
2 parents 7b3c0e5 + c49ff1c commit 2f5427e

File tree

5 files changed

+211
-0
lines changed

5 files changed

+211
-0
lines changed

alien-dictionary/hyer0705.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
export class Solution {
2+
alienOrder(words: string[]): string {
3+
const indegree = new Map<string, number>();
4+
const graph = new Map<string, Set<string>>();
5+
6+
for (const word of words) {
7+
for (const ch of word) {
8+
if (graph.has(ch)) continue;
9+
graph.set(ch, new Set<string>());
10+
indegree.set(ch, 0);
11+
}
12+
}
13+
14+
for (let i = 0; i < words.length - 1; i++) {
15+
const word1 = words[i];
16+
const word2 = words[i + 1];
17+
18+
let pointer = 0;
19+
while (pointer < word1.length && pointer < word2.length && word1[pointer] === word2[pointer]) {
20+
pointer++;
21+
}
22+
23+
if (pointer < word1.length && pointer === word2.length) {
24+
return "";
25+
}
26+
27+
if (pointer < word1.length && pointer < word2.length) {
28+
const neighbors = graph.get(word1[pointer])!;
29+
if (!neighbors.has(word2[pointer])) {
30+
neighbors.add(word2[pointer]);
31+
indegree.set(word2[pointer], (indegree.get(word2[pointer]) || 0) + 1);
32+
}
33+
}
34+
}
35+
36+
const queue: string[] = [];
37+
const result: string[] = [];
38+
39+
for (const [ch, degree] of indegree) {
40+
if (degree === 0) {
41+
queue.push(ch);
42+
}
43+
}
44+
45+
while (queue.length > 0) {
46+
const current = queue.shift()!;
47+
result.push(current);
48+
49+
for (const neighbor of graph.get(current) || []) {
50+
indegree.set(neighbor, (indegree.get(neighbor) || 0) - 1);
51+
if (indegree.get(neighbor) === 0) {
52+
queue.push(neighbor);
53+
}
54+
}
55+
}
56+
57+
if (indegree.size === result.length) {
58+
return result.join("");
59+
}
60+
61+
return "";
62+
}
63+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
// 37ms
16+
// Time Complexity: O(n^2), n: 노드의 수
17+
// Space Complexity: O(h), h: 트리의 높이
18+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
19+
if (preorder.length === 0 || inorder.length === 0) return null;
20+
21+
const root = preorder[0];
22+
23+
const rootIdx = inorder.findIndex((el) => el === root);
24+
25+
const leftInorder = inorder.slice(0, rootIdx);
26+
const leftPreorder = preorder.slice(1, leftInorder.length + 1);
27+
28+
const rightInorder = inorder.slice(rootIdx + 1);
29+
const rightPreorder = preorder.slice(leftInorder.length + 1);
30+
31+
const rootNode = new TreeNode(root);
32+
33+
rootNode.left = buildTree(leftPreorder, leftInorder);
34+
rootNode.right = buildTree(rightPreorder, rightInorder);
35+
36+
return rootNode;
37+
}
38+
39+
// 3ms
40+
// Time Complexity: O(n), n: 노드의 수
41+
// Space Complexity: O(n), n: 노드의 수
42+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
43+
if (preorder.length === 0 || inorder.length === 0) return null;
44+
45+
const indexMap = new Map<number, number>();
46+
inorder.forEach((node, idx) => indexMap.set(node, idx));
47+
48+
const build = (preStart: number, preEnd: number, inStart: number, inEnd: number): TreeNode | null => {
49+
if (preStart > preEnd || inStart > inEnd) return null;
50+
51+
const rootVal = preorder[preStart];
52+
const rootIdx = indexMap.get(rootVal);
53+
54+
const leftSize = rootIdx - inStart;
55+
56+
const root = new TreeNode(rootVal);
57+
root.left = build(preStart + 1, preStart + leftSize, inStart, rootIdx - 1);
58+
root.right = build(preStart + leftSize + 1, preEnd, rootIdx + 1, inEnd);
59+
60+
return root;
61+
};
62+
63+
return build(0, preorder.length - 1, 0, inorder.length - 1);
64+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Time Complexity: O(n^2), n: s의 길이
2+
// Space Complexity: O(1)
3+
function longestPalindrome(s: string): string {
4+
const expandAroundCenter = (s: string, left: number, right: number): [number, number] => {
5+
while (left >= 0 && right < s.length && s[left] === s[right]) {
6+
left--;
7+
right++;
8+
}
9+
10+
return [left + 1, right - 1];
11+
};
12+
13+
let longest = "";
14+
for (let i = 0; i < s.length; i++) {
15+
const [s1, e1] = expandAroundCenter(s, i, i);
16+
const [s2, e2] = expandAroundCenter(s, i, i + 1);
17+
18+
const odd = e1 - s1 + 1;
19+
const even = e2 - s2 + 1;
20+
21+
if (longest.length < odd) {
22+
longest = s.slice(s1, e1 + 1);
23+
}
24+
25+
if (longest.length < even) {
26+
longest = s.slice(s2, e2 + 1);
27+
}
28+
}
29+
30+
return longest;
31+
}

rotate-image/hyer0705.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
Do not return anything, modify matrix in-place instead.
3+
*/
4+
// Time Complexity: O(n^2), n: matrix의 한 변의 길이
5+
// Space Complexity: O(1)
6+
function rotate(matrix: number[][]): void {
7+
const n = matrix.length;
8+
9+
for (let i = 0; i < n; i++) {
10+
for (let j = i + 1; j < n; j++) {
11+
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
12+
}
13+
}
14+
15+
for (let i = 0; i < n; i++) {
16+
for (let j = 0; j < Math.floor(n / 2); j++) {
17+
[matrix[i][j], matrix[i][n - j - 1]] = [matrix[i][n - j - 1], matrix[i][j]];
18+
}
19+
}
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
// Time Complexity: O(m * n), m: root의 노드 수, n: subRoot의 노드 수
16+
// Space Complexity: O(h), h: root의 높이
17+
function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean {
18+
if (!root) return false;
19+
20+
const isSameTree = (root: TreeNode | null, subRoot: TreeNode | null): boolean => {
21+
if (!root && !subRoot) return true;
22+
if (!root || !subRoot) return false;
23+
if (root.val !== subRoot.val) return false;
24+
25+
return isSameTree(root.left, subRoot.left) && isSameTree(root.right, subRoot.right);
26+
};
27+
28+
const current = isSameTree(root, subRoot);
29+
const left = isSubtree(root.left, subRoot);
30+
const right = isSubtree(root.right, subRoot);
31+
32+
return current || left || right;
33+
}

0 commit comments

Comments
 (0)