Skip to content
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

[재호] WEEK 11 Solutions #545

Merged
merged 5 commits into from
Oct 27, 2024
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
63 changes: 63 additions & 0 deletions insert-interval/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* TC: O(N)
* 1번에서 N만큼 순회
* 2번에서 최대 N만큼 순회
*
* SC: O(1)
* 겹치는 interval의 시작, 끝 index를 저장하는 변수만 사용
*
* N: intervals.length
*/

/**
* @param {number[][]} intervals
* @param {number[]} newInterval
* @return {number[][]}
*/
var insert = function (intervals, newInterval) {
if (intervals.length === 0) {
return [newInterval];
}
if (newInterval[1] < intervals[0][0]) {
return [newInterval, ...intervals];
}
if (intervals[intervals.length - 1][1] < newInterval[0]) {
return [...intervals, newInterval];
}

// 1. 겹치는 interval의 시작, 끝 index를 구한다.
let overLappingStartIndex = intervals.length - 1;
let overLappingEndIndex = 0;
for (let index = 0; index < intervals.length; index++) {
const interval = intervals[index];
if (newInterval[0] <= interval[1]) {
overLappingStartIndex = Math.min(overLappingStartIndex, index);
}
if (interval[0] <= newInterval[1]) {
overLappingEndIndex = Math.max(overLappingEndIndex, index);
}
}

// 2.
// 시작index 전까지 모두 추가하고
// 시작index, 끝index의 최대 범위 interval을 추가하고
// 끝index 이후 interval을 모두 추가한다.
const result = [];
for (let index = 0; index < overLappingStartIndex; index++) {
result.push(intervals[index]);
}
const overLappedStart = Math.min(
intervals[overLappingStartIndex][0],
newInterval[0]
);
const overLappedEnd = Math.max(
intervals[overLappingEndIndex][1],
newInterval[1]
);
result.push([overLappedStart, overLappedEnd]);
for (let index = overLappingEndIndex + 1; index < intervals.length; index++) {
result.push(intervals[index]);
}

return result;
};
46 changes: 46 additions & 0 deletions maximum-depth-of-binary-tree/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* TC: O(N)
* 모든 노드를 순회합니다.
*
* SC: O(N)
* 노드의 수에 비례해서 queue의 길이가 증가됩니다.
*
* N: count of tree node
*/

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function (root) {
let maximumDepth = 0;

const queue = [[root, 1]];
while (queue.length > 0) {
const [current, depth] = queue.shift();

if (current === null) {
break;
}

maximumDepth = Math.max(maximumDepth, depth);

if (current.left) {
queue.push([current.left, depth + 1]);
}

if (current.right) {
queue.push([current.right, depth + 1]);
}
}

return maximumDepth;
};
56 changes: 56 additions & 0 deletions reorder-list/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* TC: O(N)
* 1번에서 절반 길이만큼 순회
* 2번에서 절반 길이만큼 순회
* 3번에서 절반 길이만큼 순회
*
* SC: O(1)
* linked list의 node를 가리키는 포인터를 가지고 활용하므로 linked list의 길이와 무관한 공간 복잡도를 갖습니다.
*
* N: linked list length
*/

/**
* 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) {
// 1. linked list 절반 위치 구하기
let slow = head;
let fast = head;

while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}

// 2. 후반 linked list 순서 뒤집기
let halfStartTemp = slow.next;
let halfStart = null;
// 절반을 기준으로 linked list 끊기
slow.next = null;

while (halfStartTemp) {
const temp = halfStartTemp.next;
halfStartTemp.next = halfStart;
halfStart = halfStartTemp;
halfStartTemp = temp;
}

// 3. 두 리스트 합치기
while (head && halfStart) {
const headTemp = head.next;
const halfStartTemp = halfStart.next;
head.next = halfStart;
halfStart.next = headTemp;
head = headTemp;
halfStart = halfStartTemp;
}
};