Skip to content

Commit 49315cf

Browse files
authored
Merge pull request #1049 from jdy8739/main
[jdy8739] week 12
2 parents cf5691a + 3fae094 commit 49315cf

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @return {number}
4+
*/
5+
var eraseOverlapIntervals = function (intervals) {
6+
7+
const sort = intervals.sort((a, b) => {
8+
if (a[1] === b[1]) {
9+
return a[0] - b[0];
10+
}
11+
12+
return a[1] - b[1];
13+
});
14+
15+
let count = 0;
16+
let max = sort[0][1];
17+
18+
for (let i = 0; i < sort.length - 1; i++) {
19+
const right = sort[i + 1][0];
20+
21+
if (max > right) {
22+
count++;
23+
} else {
24+
max = sort[i + 1][1];
25+
}
26+
}
27+
28+
return count;
29+
};
30+
31+
// ์‹œ๊ฐ„๋ณต์žก๋„ - O(nlogn) ์ธํ„ฐ๋ฒŒ ๋’ท ์ˆซ์ž ์ •๋ ฌ ์‹œ ์ •๋ ฌ๋กœ ์ธํ•œ ์‹œ๊ฐ„๋ณต์žก๋„ ๋ฐœ์ƒ
32+
// ๊ณต๊ฐ„๋ณต์žก๋„ - O(1) ํŠน๋ณ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์•„ ์ƒ์ˆ˜ ๊ณต๊ฐ„๋ณต์žก๋„ ๋ฐœ์ƒ
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+
* @param {ListNode} head
10+
* @param {number} n
11+
* @return {ListNode}
12+
*/
13+
var removeNthFromEnd = function (head, n) {
14+
if (!head) {
15+
return null;
16+
}
17+
18+
let node = head;
19+
20+
const list = [];
21+
22+
while (node) {
23+
list.push(node);
24+
node = node.next;
25+
}
26+
27+
const targetIndex = list.length - n - 1;
28+
29+
if (targetIndex === -1 && list.length === 1) {
30+
return null;
31+
}
32+
33+
if (targetIndex === -1) {
34+
return head.next;
35+
}
36+
37+
if (list[targetIndex]) {
38+
list[targetIndex].next = list[targetIndex]?.next?.next || null;
39+
}
40+
41+
return head;
42+
};
43+
44+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n) - ๋…ธ๋“œ๋ฅผ ํ•œ๋ฒˆ ์ˆœํšŒํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ
45+
// ๊ณต๊ฐ„๋ณต์žก๋„ O(n) - ์ˆœํšŒ ์ดํ›„์— ์ œ๊ฑฐํ•  ๋…ธ๋“œ์˜ ๋ฐ”๋กœ ์•ž ๋…ธ๋“œ๋ ˆ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅ

โ€Žsame-tree/jdy8739.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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} p
11+
* @param {TreeNode} q
12+
* @return {boolean}
13+
*/
14+
var isSameTree = function(p, q) {
15+
const dfs = (a, b) => {
16+
const isValSame = a?.val === b?.val;
17+
const isLeftValSame = a?.left?.val === b?.left?.val;
18+
const isRightValSame = a?.right?.val === b?.right?.val;
19+
20+
if (!isValSame || !isLeftValSame || !isRightValSame) {
21+
return true;
22+
}
23+
24+
if (a?.left && b?.left) {
25+
const isLeftDiff = dfs(a.left, b.left);
26+
27+
if (isLeftDiff) {
28+
return true;
29+
}
30+
}
31+
W
32+
33+
if (a?.right && b?.right) {
34+
const isRightDiff = dfs(a.right, b.right);
35+
36+
if (isRightDiff) {
37+
return true;
38+
}
39+
}
40+
41+
}
42+
43+
44+
return !dfs(p, q);
45+
};
46+
47+
// ์‹œ๊ฐ„๋ณต์žก๋„ - O(n) p์™€ q๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋ฏ€๋กœ
48+
// ๊ณต๊ฐ„๋ณต์žก๋„ - O(h) ๊นŠ์ด์šฐ์„ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ๋†’์ด๋งŒํผ ์‹คํ–‰ํ™˜๊ฒฝ์ด ํ•จ์ˆ˜ํ˜ธ์ถœ์Šคํƒ์— ์ €์žฅ๋˜๋ฏ€๋กœ

0 commit comments

Comments
ย (0)