Skip to content

Commit 681733f

Browse files
authored
Merge pull request #1028 from jdy8739/main
[jdy8739] Week 11
2 parents 687e199 + 09debd2 commit 681733f

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-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+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxPathSum = function(root) {
14+
let max = root.val;
15+
16+
const dfs = (node) => {
17+
if (!node) {
18+
return 0;
19+
}
20+
21+
const left = Math.max(dfs(node.left), 0);
22+
const right = Math.max(dfs(node.right), 0);
23+
const sum = node.val + left + right;
24+
25+
max = Math.max(sum, max);
26+
27+
return node.val + Math.max(left, right);
28+
}
29+
30+
dfs(root);
31+
32+
return max;
33+
};
34+
35+
// 시간복잡도 O(n) -> 트리의 모든 노드를 재귀적으로 탐색하므로 복잡도는 노드의 수와 비례함
36+
// 공간복잡도 O(h) -> 입력된 트리의 최대 높이만큼 재귀 스택이 쌓이므로 공간복잡도는 트리의 높이와 같음
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
var maxDepth = function (root) {
14+
let max = 0;
15+
16+
const dfs = (node, depth) => {
17+
if (node) {
18+
dfs(node.left, depth + 1);
19+
dfs(node.right, depth + 1);
20+
} else { // when this node is null
21+
max = Math.max(max, depth);
22+
}
23+
}
24+
25+
dfs(root, 0);
26+
27+
return max;
28+
};
29+
30+
// 시간복잡도 O(n) -> 트리의 모든 노드를 방문하면서 총 노드의 갯수인 n개 만큼의 시간복잡도를 가지게 되므로
31+
// 공간복잡도 O(h) -> 콜스택의 최대 길이는 트리의 깊이와 동일하므로
32+

merge-intervals/jdy8739.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @return {number[][]}
4+
*/
5+
var merge = function (intervals) {
6+
const sort = intervals.sort((a, b) => a[0] - b[0]);
7+
8+
const mergedIntervals = [sort[0]];
9+
10+
for (let i = 1; i < sort.length; i++) {
11+
/** 현재 합쳐진 인터벌의 마지막 요소 */
12+
const lastMergedInterval = mergedIntervals[mergedIntervals.length - 1];
13+
14+
const endOfMergedInterval = lastMergedInterval[1];
15+
16+
const next = sort[i][0];
17+
18+
if (endOfMergedInterval < next) {
19+
mergedIntervals.push(sort[i]);
20+
} else {
21+
lastMergedInterval[1] = Math.max(lastMergedInterval[1], sort[i][1]);
22+
}
23+
}
24+
25+
return mergedIntervals;
26+
};
27+
28+
// 시간복잡도 O(nlogn) -> sort 함수의 시간복잡도가 O(nlogn)이기 때문에
29+
// 공간복잡도 O(n) -> intervals 배열을 정렬하여 arr이라는 식별자의 배열을 만들어야 하기 때문에 필요한 공간

reorder-list/jdy8739.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
if (!head) {
14+
return null;
15+
}
16+
17+
const stack = [];
18+
19+
let node = head;
20+
21+
while (node) {
22+
stack.push(node);
23+
node = node.next;
24+
}
25+
26+
const length = stack.length;
27+
28+
node = head;
29+
let count = 0;
30+
31+
while (count < length) {
32+
if (count % 2 === 0) {
33+
const top = stack.pop();
34+
35+
top.next = node.next;
36+
37+
node.next = top;
38+
}
39+
40+
if (count === length - 1) {
41+
node.next = null;
42+
} else {
43+
node = node.next;
44+
}
45+
46+
count++;
47+
}
48+
49+
50+
return head;
51+
};
52+
53+
// 시간복잡도 O(n) -> while문이 링크드리스트의 길이만큼 순회를하기때문에 링크드리스트의 길이만큼 시간이 걸림
54+
// 공간복잡도 O(n) -> 스택에 모든 노드를 저장하기 때문에 링크드리스트의 길이만큼 공간이 필요

0 commit comments

Comments
 (0)