Skip to content

Commit 604f33a

Browse files
authored
Merge pull request #1663 from uraflower/main
[uraflower] Week 15 Solutions
2 parents 2236316 + b12780a commit 604f33a

File tree

5 files changed

+258
-0
lines changed

5 files changed

+258
-0
lines changed

โ€Žalien-dictionary/uraflower.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @param words: a list of words
3+
* @return: a string which is correct order
4+
*/
5+
const alienOrder = (words) => {
6+
const graph = {};
7+
8+
// ์ดˆ๊ธฐํ™”
9+
for (const word of words) {
10+
for (const char of word) {
11+
if (!graph[char]) {
12+
graph[char] = new Set();
13+
}
14+
}
15+
}
16+
17+
// ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
18+
for (let i = 1; i < words.length; i++) {
19+
const prev = words[i - 1];
20+
const current = words[i];
21+
let found = false;
22+
23+
const minLen = Math.min(prev.length, current.length);
24+
for (let j = 0; j < minLen; j++) {
25+
if (prev[j] !== current[j]) {
26+
graph[prev[j]].add(current[j]);
27+
found = true;
28+
break;
29+
}
30+
}
31+
// ๋ชจ์ˆœ ์ฒ˜๋ฆฌ
32+
if (!found && prev.length > current.length) {
33+
return '';
34+
}
35+
}
36+
37+
// ํƒ์ƒ‰
38+
const output = [];
39+
const visiting = new Set();
40+
const visited = new Set();
41+
42+
function dfs(current) {
43+
if (visiting.has(current)) {
44+
return false;
45+
}
46+
if (visited.has(current)) {
47+
return true;
48+
}
49+
50+
visiting.add(current);
51+
for (const adj of graph[current]) {
52+
if (!dfs(adj)) {
53+
return false;
54+
}
55+
}
56+
visiting.delete(current);
57+
58+
visited.add(current);
59+
output.push(current);
60+
return true;
61+
}
62+
63+
// ์ˆœํšŒ
64+
for (const node in graph) {
65+
if (!dfs(node)) {
66+
return '';
67+
}
68+
}
69+
70+
return output.reverse().join('');
71+
}
72+
73+
74+
// ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๋กœ ์„ ํ–‰๋˜๋Š” ์ˆœ์„œ๋ฅผ ํ‘œํ˜„
75+
// ๋น„์Šทํ•œ ๋ฌธ์ œ: https://leetcode.com/problems/course-schedule/description/
76+
77+
// ์œ„์ƒ์ •๋ ฌ ์‚ฌ์šฉํ•ด์„œ ์ง„์ž…์ฐจ์ˆ˜ ๊ธฐ์ค€ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Œ
78+
// ๋„ˆ๋ฌด ์–ด๋ ค์› ๋‹ค...
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// ์ ‘๊ทผ๋ฒ•
2+
// preorder: v l r
3+
// inorder: l v r
4+
5+
// preorder์˜ ๊ฐ€์žฅ ์ฒซ ์š”์†Œ๋Š” ๋ฌด์กฐ๊ฑด root
6+
// inorder์—์„œ root๋ณด๋‹ค ์™ผ์ชฝ์— ์žˆ๋Š” ์š”์†Œ๋Š” ์ „๋ถ€ left์ž„
7+
// ์—ฌ๊ธฐ์„œ ์ค‘๋ณต๋˜๋Š” value๊ฐ€ ์—†์–ด์•ผ ํ•˜๋Š”๋ฐ ๋ฌธ์ œ์—์„œ ์—†์Œ์„ ๋ณด์žฅํ•จ
8+
// ๋”ฐ๋ผ์„œ preorder[0]์„ inorder์—์„œ ์ฐพ๊ณ 
9+
// ๊ทธ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐฐ์—ด์„ ๋‚˜๋ˆ ์„œ ์ด๊ฑธ ๋ฐ˜๋ณต
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* function TreeNode(val, left, right) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.left = (left===undefined ? null : left)
16+
* this.right = (right===undefined ? null : right)
17+
* }
18+
*/
19+
/**
20+
* @param {number[]} preorder
21+
* @param {number[]} inorder
22+
* @return {TreeNode}
23+
*/
24+
const buildTree = function (preorder, inorder) {
25+
let preorderIndex = 0;
26+
const map = inorder.reduce((map, val, index) => {
27+
map[val] = index;
28+
return map;
29+
}, {});
30+
31+
function build(start, end) {
32+
if (start > end) {
33+
return null;
34+
}
35+
36+
const root = new TreeNode(preorder[preorderIndex++]);
37+
const index = map[root.val];
38+
39+
root.left = build(start, index - 1);
40+
root.right = build(index + 1, end);
41+
42+
return root;
43+
}
44+
45+
return build(0, inorder.length - 1);
46+
};
47+
48+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
49+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
const longestPalindrome = function(s) {
6+
let substr = '';
7+
8+
for (let i = 0; i < s.length; i++) {
9+
const oddStr = getCurrentLongestPalindrome(i, i); // s[i] ๊ธฐ์ค€ ์–‘์˜†์œผ๋กœ ๋ป—์–ด๋‚˜๊ฐ => length๋Š” ํ•ญ์ƒ ํ™€์ˆ˜
10+
const evenStr = getCurrentLongestPalindrome(i, i+1); // s[i] + s[i+1] ๊ธฐ์ค€ ์–‘์˜†์œผ๋กœ => length๋Š” ํ•ญ์ƒ ์ง์ˆ˜
11+
12+
if (substr.length < oddStr.length) substr = oddStr;
13+
if (substr.length < evenStr.length) substr = evenStr;
14+
}
15+
16+
// ๊ธฐ์ค€ ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋ฉด์„œ ๊ฐ€์žฅ ๊ธด ํŒฐ๋ฆฐ๋“œ๋กฌ์„ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ (๊ธฐ์ค€ ๋ฌธ์ž์—ด: s.slice(l, r + 1))
17+
const getCurrentLongestPalindrome = function (l, r) {
18+
while (0 <= l && r < s.length && s[l] === s[r]) {
19+
l--;
20+
r++;
21+
}
22+
23+
return s.slice(l+1, r);
24+
}
25+
26+
return substr;
27+
};
28+
29+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n^2)
30+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(s) (s: substr.length)

โ€Žrotate-image/uraflower.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
const rotate = function (matrix) {
6+
const n = matrix.length;
7+
8+
// ์ƒํ•˜์ขŒ์šฐ ๊ฒ‰์—์„œ๋ถ€ํ„ฐ ํ•œ ๊ฒน์”ฉ ์•ˆ์œผ๋กœ ์ง„์ž…
9+
for (let layer = 0; layer < Math.floor(n / 2); layer++) {
10+
11+
// layer ์œ„์˜ ๊ฐ ์นธ๋“ค์„ rotate
12+
// ํ•˜๋‚˜๋ฅผ rotateํ–ˆ์„ ๋•Œ ์—ฐ์‡„์ ์œผ๋กœ rotate๋˜๋Š” ๊ทœ์น™์„ ์ด์šฉํ•ด
13+
// ๋„ค ์นธ rotateํ•˜๋Š” ๊ฑธ x๋ฒˆ ๋ฐ˜๋ณตํ•จ
14+
15+
// x = n - 2 * layer - 1
16+
// ์ „์ฒด ๋ฐฐ์—ด ํฌ๊ธฐ n์—์„œ layer๋งŒํผ ์•ˆ์ชฝ์œผ๋กœ ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋Š”๋ฐ
17+
// ์ƒํ•˜, ์ขŒ์šฐ๋งŒํผ ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋‹ˆ๊นŒ 2๋ฅผ ๊ณฑํ•จ
18+
// 1์„ ์•ˆ ๋นผ๋ฉด ์ด๋ฏธ rotateํ•œ ์ž๋ฆฌ๋ฅผ ๋‹ค์‹œ rotateํ•˜๋ฏ€๋กœ ๋นผ์คŒ
19+
20+
for (let i = 0; i < n - 2 * layer - 1; i++) {
21+
const top = layer;
22+
const bottom = n - 1 - layer;
23+
const left = top;
24+
const right = bottom;
25+
26+
const topLeft = matrix[top][left + i];
27+
matrix[top][left + i] = matrix[bottom - i][left];
28+
matrix[bottom - i][left] = matrix[bottom][right - i];
29+
matrix[bottom][right - i] = matrix[top + i][right];
30+
matrix[top + i][right] = topLeft;
31+
}
32+
}
33+
};
34+
35+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n^2)
36+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
37+
38+
// ์ฐธ๊ณ ๋กœ ์ค‘์ฒฉ ๊ตฌ์กฐ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊ฟ”๋„ ๋จ
39+
const top = 0;
40+
const bottom = n - 1;
41+
42+
while (top < bototm) {
43+
const left = top;
44+
const right = bottom;
45+
46+
for (let i = top; i < bottom; i ++) {
47+
// rotate
48+
}
49+
50+
top++;
51+
bottom--;
52+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @param {TreeNode} subRoot
12+
* @return {boolean}
13+
*/
14+
const isSubtree = function(root, subRoot) {
15+
const isSame = function(node1, node2) {
16+
if (!node1 && !node2) return true;
17+
if (node1?.val !== node2?.val) return false;
18+
19+
return isSame(node1.left, node2.left) && isSame(node1.right, node2.right);
20+
}
21+
22+
const queue = [root];
23+
24+
while (queue.length) {
25+
const node = queue.shift();
26+
27+
if (node.left) {
28+
queue.push(node.left);
29+
}
30+
31+
if (node.right) {
32+
queue.push(node.right);
33+
}
34+
35+
if (isSame(node, subRoot)) {
36+
return true;
37+
}
38+
}
39+
40+
return false;
41+
};
42+
43+
// ๋‹ค๋ฅธ ์ ‘๊ทผ๋ฒ•:
44+
// ์ •๋ ฌํ•ด์„œ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹ => left, right ๊ตฌ๋ถ„์ด ์•ˆ๊ฐ€๋Š” ๋ฌธ์ œ
45+
// ์ •๋ ฌํ•  ๋•Œ left, right ์ •๋ณด๋ฅผ ํฌํ•จํ•ด์„œ ์ง๋ ฌํ™”ํ•˜๋ฉด ๋จ
46+
// ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ณต์žก๋„ ๋ฉด์—์„œ ์„ฑ๋Šฅ์ด ๋” ์ข‹์Œ
47+
48+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n * m) (n: root size, m: subroot size)
49+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n + m)

0 commit comments

Comments
ย (0)