Skip to content

Commit 9f4fc39

Browse files
authored
Merge pull request #1582 from hsskey/main
[hsskey] WEEK 11 Solutions
2 parents 89ef0f5 + 46b01c0 commit 9f4fc39

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
/**
11+
* @param {TreeNode} root
12+
* @return {number}
13+
*/
14+
var maxPathSum = function(root) {
15+
let res = [root.val];
16+
17+
function dfs(node) {
18+
if (!node) return 0;
19+
20+
let leftMax = dfs(node.left);
21+
let rightMax = dfs(node.right);
22+
23+
leftMax = Math.max(leftMax, 0);
24+
rightMax = Math.max(rightMax, 0);
25+
26+
// 경유지점 포함한 경로 최대값 업데이트
27+
res[0] = Math.max(res[0], node.val + leftMax + rightMax);
28+
29+
// 분기하지 않는 경로에서의 최대값 리턴
30+
return node.val + Math.max(leftMax, rightMax);
31+
}
32+
33+
dfs(root);
34+
return res[0];
35+
};
36+

graph-valid-tree/hsskey.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export class Solution {
2+
/**
3+
* @param {number} n - number of nodes
4+
* @param {number[][]} edges - undirected edges
5+
* @return {boolean}
6+
*/
7+
validTree(n, edges) {
8+
if (n === 0) return true;
9+
10+
// 인접 리스트 생성
11+
const adj = {};
12+
for (let i = 0; i < n; i++) {
13+
adj[i] = [];
14+
}
15+
for (const [n1, n2] of edges) {
16+
adj[n1].push(n2);
17+
adj[n2].push(n1);
18+
}
19+
20+
const visit = new Set();
21+
22+
const dfs = (i, prev) => {
23+
if (visit.has(i)) return false;
24+
25+
visit.add(i);
26+
27+
for (const j of adj[i]) {
28+
if (j === prev) continue;
29+
if (!dfs(j, i)) return false;
30+
}
31+
32+
return true;
33+
};
34+
35+
return dfs(0, -1) && visit.size === n;
36+
}
37+
}
38+

merge-intervals/hsskey.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @return {number[][]}
4+
*/
5+
var merge = function(intervals) {
6+
if (intervals.length === 0) return [];
7+
8+
// 시작값 기준으로 정렬
9+
intervals.sort((a, b) => a[0] - b[0]);
10+
11+
const output = [intervals[0]];
12+
13+
for (let i = 1; i < intervals.length; i++) {
14+
const [start, end] = intervals[i];
15+
const lastEnd = output[output.length - 1][1];
16+
17+
if (start <= lastEnd) {
18+
output[output.length - 1][1] = Math.max(lastEnd, end);
19+
} else {
20+
output.push([start, end]);
21+
}
22+
}
23+
24+
return output;
25+
};
26+

missing-number/hsskey.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var missingNumber = function(nums) {
6+
let res = nums.length;
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
res += i - nums[i];
10+
}
11+
12+
return res;
13+
};
14+

reorder-list/hsskey.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
/**
10+
* @param {ListNode} head
11+
* @return {void} Do not return anything, modify head in-place instead.
12+
*/
13+
var reorderList = function(head) {
14+
if (!head || !head.next) return;
15+
16+
let slow = head, fast = head.next;
17+
while (fast && fast.next) {
18+
slow = slow.next;
19+
fast = fast.next.next;
20+
}
21+
22+
let second = slow.next;
23+
let prev = null;
24+
slow.next = null;
25+
while (second) {
26+
let tmp = second.next;
27+
second.next = prev;
28+
prev = second;
29+
second = tmp;
30+
}
31+
32+
let first = head;
33+
second = prev;
34+
while (second) {
35+
let tmp1 = first.next;
36+
let tmp2 = second.next;
37+
38+
first.next = second;
39+
second.next = tmp1;
40+
41+
first = tmp1;
42+
second = tmp2;
43+
}
44+
};
45+

0 commit comments

Comments
 (0)