-
-
Notifications
You must be signed in to change notification settings - Fork 245
[재호] WEEK 11 Solutions #545
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* 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 ( | ||
(interval[0] <= newInterval[0] && newInterval[0] <= interval[1]) || | ||
newInterval[0] < interval[0] | ||
) { | ||
overLappingStartIndex = Math.min(overLappingStartIndex, index); | ||
} | ||
if ( | ||
(interval[0] <= newInterval[1] && newInterval[1] <= interval[1]) || | ||
interval[1] < 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; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.