Skip to content

Commit e9aacc3

Browse files
authored
Merge pull request #1567 from byol-han/main
[byol-han] WEEK 11 solutions
2 parents 4d40f78 + 960ff46 commit e9aacc3

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* https://leetcode.com/problems/binary-tree-maximum-path-sum/
3+
* Definition for a binary tree node.
4+
* function TreeNode(val, left, right) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.left = (left===undefined ? null : left)
7+
* this.right = (right===undefined ? null : right)
8+
* }
9+
*/
10+
/**
11+
* @param {TreeNode} root
12+
* @return {number}
13+
*/
14+
var maxPathSum = function (root) {
15+
let maxSum = -Infinity; // global max
16+
17+
function dfs(node) {
18+
if (!node) return 0;
19+
20+
// 왼쪽과 오른쪽 서브트리에서 최대 경로 합을 구한다
21+
// 음수면 0으로 치환 (해당 서브트리를 포함하지 않는게 더 이득인 경우)
22+
let leftMax = Math.max(0, dfs(node.left));
23+
let rightMax = Math.max(0, dfs(node.right));
24+
25+
// 현재 노드를 루트로 하는 경로에서 최대값을 계산 (left + node + right)
26+
let currentMax = leftMax + node.val + rightMax;
27+
28+
// global 최대값 갱신
29+
maxSum = Math.max(maxSum, currentMax);
30+
31+
// 부모 노드로 return 시: 한쪽 방향으로만 선택 가능
32+
return node.val + Math.max(leftMax, rightMax);
33+
}
34+
35+
dfs(root);
36+
return maxSum;
37+
};

missing-number/byol-han.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* https://leetcode.com/problems/missing-number/
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
var missingNumber = function (nums) {
7+
nums.sort((a, b) => a - b);
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
if (nums[i] !== i) {
11+
return i; // 빠진 숫자를 찾으면 리턴
12+
}
13+
}
14+
15+
return nums.length; // 모든 숫자가 다 있으면 빠진 건 n
16+
};
17+
18+
// 수학적 합 공식 이용하기 (가장 빠름)
19+
//시간복잡도: O(n)
20+
// 공간복잡도: O(1) (아주 효율적)
21+
22+
var missingNumber = function (nums) {
23+
const n = nums.length;
24+
const expectedSum = (n * (n + 1)) / 2;
25+
const actualSum = nums.reduce((a, b) => a + b, 0);
26+
return expectedSum - actualSum;
27+
};

reorder-list/byol-han.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
*https://leetcode.com/problems/reorder-list/
3+
* Definition for singly-linked list.
4+
* function ListNode(val, next) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.next = (next===undefined ? null : next)
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+
// 1. 중간 지점 찾기 (slow, fast 포인터 사용)
17+
let slow = head;
18+
let fast = head;
19+
while (fast.next && fast.next.next) {
20+
slow = slow.next;
21+
fast = fast.next.next;
22+
}
23+
24+
// 2. 중간 이후 리스트 뒤집기
25+
let prev = null;
26+
let curr = slow.next;
27+
while (curr) {
28+
let nextTemp = curr.next;
29+
curr.next = prev;
30+
prev = curr;
31+
curr = nextTemp;
32+
}
33+
// 중간 지점 이후는 끊기
34+
slow.next = null;
35+
36+
// 3. 앞쪽 리스트와 뒤쪽 리스트 교차 병합
37+
let first = head;
38+
let second = prev;
39+
while (second) {
40+
let tmp1 = first.next;
41+
let tmp2 = second.next;
42+
43+
first.next = second;
44+
second.next = tmp1;
45+
46+
first = tmp1;
47+
second = tmp2;
48+
}
49+
};

same-tree/byol-han.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* https://leetcode.com/problems/same-tree/description/
3+
* Definition for a binary tree node.
4+
* function TreeNode(val, left, right) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.left = (left===undefined ? null : left)
7+
* this.right = (right===undefined ? null : right)
8+
* }
9+
*/
10+
/**
11+
* @param {TreeNode} p
12+
* @param {TreeNode} q
13+
* @return {boolean}
14+
*/
15+
var isSameTree = function (p, q) {
16+
// 둘 다 null이면 같은 트리
17+
if (p === null && q === null) return true;
18+
19+
// 하나는 null이고 하나는 값이 있다면 다른 트리
20+
if (p === null || q === null) return false;
21+
22+
// 값이 다르면 다른 트리
23+
if (p.val !== q.val) return false;
24+
25+
// 왼쪽과 오른쪽 서브트리도 각각 재귀적으로 비교
26+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
27+
};

0 commit comments

Comments
 (0)