Skip to content

Commit 106acb9

Browse files
authored
Merge pull request #1053 from gwbaik9717/main
[ganu] Week 12
2 parents ce836d3 + 77309a4 commit 106acb9

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Time complexity: O(nlogn)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* @param {number[][]} intervals
6+
* @return {number}
7+
*/
8+
var eraseOverlapIntervals = function (intervals) {
9+
intervals.sort((a, b) => {
10+
if (a[0] === b[0]) {
11+
return a[1] - b[1];
12+
}
13+
14+
return a[0] - b[0];
15+
});
16+
17+
let count = 0;
18+
let prevEnd = intervals[0][1];
19+
20+
for (let i = 1; i < intervals.length; i++) {
21+
const [start, end] = intervals[i];
22+
23+
// 구간이 겹칠 때
24+
if (prevEnd > start) {
25+
count++;
26+
prevEnd = Math.min(prevEnd, end);
27+
continue;
28+
}
29+
30+
// 구간이 겹치지 않을 때
31+
prevEnd = end;
32+
}
33+
34+
return count;
35+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* function ListNode(val, next) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
*/
11+
/**
12+
* @param {ListNode} head
13+
* @param {number} n
14+
* @return {ListNode}
15+
*/
16+
var removeNthFromEnd = function (head, n) {
17+
const reverse = (head) => {
18+
let next = null;
19+
let current = head;
20+
21+
while (current) {
22+
const temp = current.next;
23+
current.next = next;
24+
next = current;
25+
current = temp;
26+
}
27+
28+
return next;
29+
};
30+
31+
// Reverse
32+
let reversedHead = reverse(head);
33+
34+
if (n === 1) {
35+
reversedHead = reversedHead.next;
36+
} else {
37+
let prev = null;
38+
let current = reversedHead;
39+
40+
for (let i = 1; i < n; i++) {
41+
prev = current;
42+
current = current.next;
43+
}
44+
45+
prev.next = current.next;
46+
}
47+
48+
// Reverse Again
49+
return reverse(reversedHead);
50+
};

same-tree/gwbaik9717.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// n: number of nodes
2+
// Time complexity: O(n)
3+
// Space complexity: O(n)
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* function TreeNode(val, left, right) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
*/
13+
/**
14+
* @param {TreeNode} p
15+
* @param {TreeNode} q
16+
* @return {boolean}
17+
*/
18+
var isSameTree = function (p, q) {
19+
const dfs = (currentP, currentQ) => {
20+
if (!currentP && !currentQ) {
21+
return true;
22+
}
23+
24+
if ((!currentP && currentQ) || (currentP && !currentQ)) {
25+
return false;
26+
}
27+
28+
if (currentP.val !== currentQ.val) {
29+
return false;
30+
}
31+
32+
return (
33+
dfs(currentP.left, currentQ.left) && dfs(currentP.right, currentQ.right)
34+
);
35+
};
36+
37+
return dfs(p, q);
38+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// n: number of nodes, h: height of tree (max: n)
2+
// Time complexity: O(n)
3+
// Space complexity: O(2^h)
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* function TreeNode(val) {
8+
* this.val = val;
9+
* this.left = this.right = null;
10+
* }
11+
*/
12+
13+
class _Queue {
14+
constructor() {
15+
this.q = [];
16+
this.left = 0;
17+
this.right = 0;
18+
}
19+
20+
push(value) {
21+
this.q.push(value);
22+
this.right++;
23+
}
24+
25+
shift() {
26+
const rv = this.q[this.left];
27+
delete this.q[this.left++];
28+
29+
return rv;
30+
}
31+
32+
isEmpty() {
33+
return this.left === this.right;
34+
}
35+
}
36+
37+
const isValid = (data) => {
38+
if (data === undefined || data === null) {
39+
return false;
40+
}
41+
42+
return true;
43+
};
44+
45+
/**
46+
* Encodes a tree to a single string.
47+
*
48+
* @param {TreeNode} root
49+
* @return {string}
50+
*/
51+
var serialize = function (root) {
52+
const answer = [null];
53+
54+
const bfs = (current) => {
55+
const q = new _Queue();
56+
q.push([1, current]);
57+
58+
while (!q.isEmpty()) {
59+
const [i, current] = q.shift();
60+
61+
if (current === null) {
62+
answer[i] = current;
63+
continue;
64+
}
65+
66+
answer[i] = current.val;
67+
68+
const left = 2 * i;
69+
const right = left + 1;
70+
71+
if (current.left) {
72+
q.push([left, current.left]);
73+
} else {
74+
q.push([left, null]);
75+
}
76+
77+
if (current.right) {
78+
q.push([right, current.right]);
79+
} else {
80+
q.push([right, null]);
81+
}
82+
}
83+
};
84+
85+
bfs(root);
86+
87+
while (answer.length > 1 && !isValid(answer.at(-1))) {
88+
answer.pop();
89+
}
90+
91+
return answer;
92+
};
93+
94+
/**
95+
* Decodes your encoded data to tree.
96+
*
97+
* @param {string} data
98+
* @return {TreeNode}
99+
*/
100+
var deserialize = function (data) {
101+
if (data.length === 1) {
102+
return null;
103+
}
104+
105+
const root = new TreeNode(data[1]);
106+
const q = new _Queue();
107+
q.push([1, root]);
108+
109+
while (!q.isEmpty()) {
110+
const [i, current] = q.shift();
111+
112+
const left = i * 2;
113+
const right = left + 1;
114+
115+
if (left <= data.length && isValid(data[left])) {
116+
current.left = new TreeNode(data[left]);
117+
q.push([left, current.left]);
118+
}
119+
120+
if (right <= data.length && isValid(data[right])) {
121+
current.right = new TreeNode(data[right]);
122+
q.push([right, current.right]);
123+
}
124+
}
125+
126+
return root;
127+
};
128+
129+
/**
130+
* Your functions will be called as such:
131+
* deserialize(serialize(root));
132+
*/

0 commit comments

Comments
 (0)