Skip to content

Commit e6bf845

Browse files
authored
Merge pull request #1573 from lhc0506/main
[lhc0506] Week 11 Solutions
2 parents 098c1b0 + dd1981a commit e6bf845

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

merge-intervals/lhc0506.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @return {number[][]}
4+
*/
5+
var merge = function(intervals) {
6+
const sortedIntervals = intervals.toSorted((a, b) => a[0] - b[0]);
7+
8+
if (intervals.length === 1) {
9+
return intervals;
10+
}
11+
12+
const result = [];
13+
let [start, end] = sortedIntervals[0];
14+
15+
16+
for (let i = 1; i < sortedIntervals.length; i++) {
17+
const [currentStart, currentEnd] = sortedIntervals[i];
18+
19+
if (currentStart <= end) {
20+
end = Math.max(end, currentEnd);
21+
} else {
22+
result.push([start, end]);
23+
start = currentStart;
24+
end = currentEnd;
25+
}
26+
27+
if (i === sortedIntervals.length - 1) {
28+
result.push([start, end]);
29+
}
30+
}
31+
32+
return result;
33+
};
34+
35+
// 시간 복잡도: O(nlogn)
36+
// 공간 복잡도: O(n)

missing-number/lhc0506.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var missingNumber = function(nums) {
6+
const n = nums.length;
7+
const array = new Array(n).fill(false);
8+
9+
for (let i = 0; i < n; i++) {
10+
array[nums[i]] = true;
11+
}
12+
13+
const index = array.findIndex(item => item === false);
14+
return index === -1 ? n : index;
15+
};
16+
17+
// 시간 복잡도: O(n)
18+
// 공간 복잡도: O(n)

reorder-list/lhc0506.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
const nodes = {};
14+
let currentNode = head;
15+
let i = 0;
16+
while (currentNode) {
17+
nodes[i] = currentNode;
18+
currentNode = currentNode.next;
19+
i++;
20+
}
21+
22+
i--;
23+
24+
for (let j = 0; j < (i / 2); j++) {
25+
nodes[j].next = nodes[i - j];
26+
nodes[i - j].next = nodes[j + 1];
27+
console.log(j)
28+
}
29+
30+
nodes[Math.ceil(i / 2)].next = null;
31+
};
32+
33+
// 시간 복잡도: O(n)
34+
// 공간 복잡도: O(n)

0 commit comments

Comments
 (0)