Skip to content

Commit 7a299ac

Browse files
authored
Merge pull request #1570 from uraflower/main
[uraflower] Week11 Solutions
2 parents cc24632 + 1ef9a64 commit 7a299ac

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed
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+
* 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+
* @return {number}
12+
*/
13+
const maxPathSum = function (root) {
14+
let max = root.val;
15+
16+
function getMaxSum(node) {
17+
if (!node) return 0;
18+
19+
const leftVal = Math.max(getMaxSum(node.left), 0);
20+
const rightVal = Math.max(getMaxSum(node.right), 0);
21+
22+
max = Math.max(node.val + leftVal + rightVal, max);
23+
24+
return node.val + Math.max(leftVal, rightVal);
25+
}
26+
27+
getMaxSum(root);
28+
29+
return max;
30+
};
31+
32+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
33+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(h) (h: ํŠธ๋ฆฌ์˜ ๋†’์ด, ์ฆ‰ ์žฌ๊ท€ ์Šคํƒ ๊นŠ์ด)

โ€Žgraph-valid-tree/uraflower.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} edges
4+
* @returns {boolean}
5+
*/
6+
function validTree(n, edges) {
7+
// ํŠธ๋ฆฌ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ™•์ธ(V = E + 1)
8+
if (edges.length !== n - 1) {
9+
return false;
10+
}
11+
12+
const graph = Array.from({ length: n }).map(() => []);
13+
14+
for (const [u, v] of edges) {
15+
graph[u].push(v);
16+
graph[v].push(u);
17+
}
18+
19+
const queue = [[-1, 0]]; // ๋ถ€๋ชจ ๋…ธ๋“œ, ์ž์‹ ๋…ธ๋“œ
20+
const visited = new Set();
21+
22+
while (queue.length) {
23+
const [parent, current] = queue.shift();
24+
if (visited.has(current)) {
25+
return false;
26+
}
27+
visited.add(current);
28+
29+
for (const neighbor of graph[current]) {
30+
if (neighbor === parent) continue;
31+
queue.push([current, neighbor]);
32+
}
33+
}
34+
35+
return visited.size === n;
36+
}
37+
38+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(V + E)
39+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(V)

โ€Žmerge-intervals/uraflower.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @return {number[][]}
4+
*/
5+
const merge = function (intervals) {
6+
// ์‹œ์ž‘์  ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
7+
intervals.sort((a, b) => Number(a[0]) - Number(b[0]));
8+
const result = [];
9+
10+
for (const current of intervals) {
11+
const last = result[result.length - 1];
12+
13+
// ๊ฒน์น˜๋Š” ๊ตฌ๊ฐ„์ด ์žˆ์œผ๋ฉด, ๊ตฌ๊ฐ„์˜ ๋์ ์„ ๋” ํฐ ๊ฒƒ์œผ๋กœ ๋ฎ์–ด์”Œ์šฐ๊ธฐ
14+
if (last && current[0] <= last[1]) {
15+
result[result.length - 1][1] = Math.max(current[1], last[1]);
16+
}
17+
// ๊ฒน์น˜๋Š” ๊ตฌ๊ฐ„์ด ์—†์œผ๋ฉด, ์ƒˆ๋กœ ์ง‘์–ด ๋„ฃ๊ธฐ
18+
else {
19+
result.push(current);
20+
}
21+
}
22+
23+
return result;
24+
};
25+
26+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n * log n)
27+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)

โ€Žmissing-number/uraflower.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const missingNumber = function(nums) {
6+
const n = nums.length;
7+
let sum = (n * (n + 1)) / 2;
8+
9+
for (let num of nums) {
10+
sum -= num;
11+
}
12+
13+
return sum;
14+
};
15+
16+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
17+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

โ€Žreorder-list/uraflower.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {void} Do not return anything, modify head in-place instead.
11+
*/
12+
var reorderList = function (head) {
13+
const nodes = {};
14+
let current = head;
15+
let n = 0; // ์ธ๋ฑ์Šค ์—ญํ• 
16+
17+
// ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ๋…ธ๋“œ๋ฅผ ๊ฐ์ฒด์— ์ €์žฅ
18+
while (current) {
19+
nodes[n] = current;
20+
current = current.next;
21+
nodes[n].next = null; // ์—ฌ๊ธฐ์„œ ๋ชจ๋“  ๋…ธ๋“œ์˜ next๋ฅผ null๋กœ ๋ณ€๊ฒฝ
22+
// ์ด๋ ‡๊ฒŒ ์•ˆ ํ•˜๋ฉด next ๋ฐ”๊พธ๊ธฐ ํ•œ ๋‹ค์Œ์— ๋งจ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ next๋งŒ null๋กœ ๋ฐ”๊ฟ”์•ผ ํ•จ
23+
n++
24+
}
25+
26+
// ์ €์žฅํ•œ ๋…ธ๋“œ๋ฅผ i, n-i, ... ์ˆœ์„œ๋กœ ์ ‘๊ทผํ•˜๋ฉด์„œ next ๋ฐ”๊พธ๊ธฐ
27+
for (let i = 0; i < Math.floor(n / 2); i++) {
28+
nodes[i].next = nodes[n - i - 1];
29+
30+
// n-i-1 === i+1 ์ธ ๊ฒฝ์šฐ, node.next = node ์ฒ˜๋Ÿผ ์…€ํ”„ ์ˆœํ™˜์ด ๋  ์ˆ˜ ์žˆ์Œ
31+
if (n - i - 1 !== i + 1) {
32+
nodes[n - i - 1].next = nodes[i + 1];
33+
}
34+
}
35+
};
36+
37+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
38+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)

0 commit comments

Comments
ย (0)