Skip to content

[lhc0506] Week 11 Solutions #1573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions merge-intervals/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @param {number[][]} intervals
* @return {number[][]}
*/
var merge = function(intervals) {
const sortedIntervals = intervals.toSorted((a, b) => a[0] - b[0]);

if (intervals.length === 1) {
return intervals;
}

const result = [];
let [start, end] = sortedIntervals[0];


for (let i = 1; i < sortedIntervals.length; i++) {
const [currentStart, currentEnd] = sortedIntervals[i];

if (currentStart <= end) {
end = Math.max(end, currentEnd);
} else {
result.push([start, end]);
start = currentStart;
end = currentEnd;
}

if (i === sortedIntervals.length - 1) {
result.push([start, end]);
}
}

return result;
};

// 시간 복잡도: O(nlogn)
// 공간 복잡도: O(n)
18 changes: 18 additions & 0 deletions missing-number/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function(nums) {
const n = nums.length;
const array = new Array(n).fill(false);

for (let i = 0; i < n; i++) {
array[nums[i]] = true;
}

const index = array.findIndex(item => item === false);
return index === -1 ? n : index;
};

// 시간 복잡도: O(n)
// 공간 복잡도: O(n)
34 changes: 34 additions & 0 deletions reorder-list/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {void} Do not return anything, modify head in-place instead.
*/
var reorderList = function(head) {
const nodes = {};
let currentNode = head;
let i = 0;
while (currentNode) {
nodes[i] = currentNode;
currentNode = currentNode.next;
i++;
}

i--;

for (let j = 0; j < (i / 2); j++) {
nodes[j].next = nodes[i - j];
nodes[i - j].next = nodes[j + 1];
console.log(j)
}

nodes[Math.ceil(i / 2)].next = null;
};

// 시간 복잡도: O(n)
// 공간 복잡도: O(n)