-
Notifications
You must be signed in to change notification settings - Fork 126
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 09 Solutions #516
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
659bf6e
solve: linked list cycle
wogha95 edb8398
solve: find minimum in rotated sorted array
wogha95 73ea4ab
solve: find minimum in rotated sorted array
wogha95 1657c59
solve: pacific atlantic water flow
wogha95 06b49b9
solve: maximum subarray
wogha95 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 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,37 @@ | ||
/** | ||
* TC: O(log N) | ||
* 문제에 명시된 log N 복잡도를 갖기위해 이진탐색 | ||
* | ||
* SC: O(1) | ||
*/ | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var findMin = function (nums) { | ||
let left = 0; | ||
let right = nums.length - 1; | ||
|
||
// case1: v_left <= v_center < v_right => return left | ||
// case2: v_right < v_left <= v_center => left = center | ||
// case3: v_center < v_right < v_left => right = center | ||
|
||
while (left < right) { | ||
const center = Math.floor((left + right) / 2); | ||
|
||
if (nums[left] <= nums[center] && nums[center] < nums[right]) { | ||
return nums[left]; | ||
} | ||
if (nums[right] < nums[left] && nums[left] <= nums[center]) { | ||
left = center + 1; | ||
continue; | ||
} | ||
if (nums[center] < nums[right] && nums[right] < nums[left]) { | ||
right = center; | ||
continue; | ||
} | ||
} | ||
|
||
return nums[left]; | ||
}; |
This file contains 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,36 @@ | ||
/** | ||
* Floyd tortoise and hare 알고리즘을 바탕으로 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오호..! 처음보는 알고리즘인데 리뷰하면서 알게됐네요 감사합니다👍 |
||
* 한칸씩 이동하는 포인터와 2칸씩 이동하는 포인터는 결국에 만난다는 점을 이용해서 품 | ||
* | ||
* TC: O(N) | ||
* SC: O(1) | ||
* N: linked list length | ||
*/ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val) { | ||
* this.val = val; | ||
* this.next = null; | ||
* } | ||
*/ | ||
|
||
/** | ||
* @param {ListNode} head | ||
* @return {boolean} | ||
*/ | ||
var hasCycle = function (head) { | ||
let oneStep = head; | ||
let twoStep = head; | ||
|
||
while (twoStep && twoStep.next) { | ||
if (oneStep === twoStep) { | ||
return true; | ||
} | ||
|
||
oneStep = oneStep.next; | ||
twoStep = twoStep.next.next; | ||
} | ||
|
||
return false; | ||
}; |
This file contains 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,24 @@ | ||
/** | ||
* 각 num을 누적된 result값에 num을 더한값과 비교해서 큰 값을 result로 유지해간다. | ||
* 그리고 최대 누적값을 구하기 위해 매 result를 구할때마다 최대 result를 갱신한다. | ||
* | ||
* TC: O(N) | ||
* SC: O(1) | ||
*/ | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxSubArray = function (nums) { | ||
let maxResult = nums[0]; | ||
let result = nums[0]; | ||
|
||
for (let index = 1; index < nums.length; index++) { | ||
const num = nums[index]; | ||
result = Math.max(result + num, num); | ||
maxResult = Math.max(maxResult, result); | ||
} | ||
|
||
return maxResult; | ||
}; |
This file contains 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,94 @@ | ||
/** | ||
* pacific(0행, 0열), atlantic(row-1행, column-1열)에서 높이가 같거나 높은 곳으로 순회한다. | ||
* 그리고 pacific에서 온 물과 atlantic에서 온 물이 만나는 곳을 정답으로 만든다. | ||
* | ||
* TC: O(row * column) | ||
* queue에 최대 row * column만큼 들어갑니다. | ||
* | ||
* SC: O(row * column) | ||
* pacific 또는 atlantic에 최대 row * column만큼 들어갑니다. | ||
*/ | ||
|
||
/** | ||
* @param {number[][]} heights | ||
* @return {number[][]} | ||
*/ | ||
var pacificAtlantic = function (heights) { | ||
const ROW = heights.length; | ||
const COLUMN = heights[0].length; | ||
const DIRECTION = [ | ||
[-1, 0], | ||
[1, 0], | ||
[0, -1], | ||
[0, 1], | ||
]; | ||
const result = []; | ||
const queue = []; | ||
|
||
for (let c = 0; c < COLUMN; c++) { | ||
queue.push([0, c]); | ||
} | ||
for (let r = 1; r < ROW; r++) { | ||
queue.push([r, 0]); | ||
} | ||
|
||
const pacific = new Set(); | ||
while (queue.length > 0) { | ||
const [row, column] = queue.shift(); | ||
pacific.add(generateKey(row, column)); | ||
for (const [directionR, directionC] of DIRECTION) { | ||
const [nextRow, nextColumn] = [row + directionR, column + directionC]; | ||
if ( | ||
isValidPosition(nextRow, nextColumn) && | ||
heights[row][column] <= heights[nextRow][nextColumn] && | ||
!pacific.has(generateKey(nextRow, nextColumn)) | ||
) { | ||
queue.push([nextRow, nextColumn]); | ||
} | ||
} | ||
} | ||
|
||
for (let c = 0; c < COLUMN; c++) { | ||
queue.push([ROW - 1, c]); | ||
} | ||
for (let r = 0; r < ROW - 1; r++) { | ||
queue.push([r, COLUMN - 1]); | ||
} | ||
|
||
const atlantic = new Set(); | ||
while (queue.length > 0) { | ||
const [row, column] = queue.shift(); | ||
const key = generateKey(row, column); | ||
atlantic.add(key); | ||
if (pacific.has(key)) { | ||
pacific.delete(key); | ||
result.push([row, column]); | ||
} | ||
for (const [directionR, directionC] of DIRECTION) { | ||
const [nextRow, nextColumn] = [row + directionR, column + directionC]; | ||
if ( | ||
isValidPosition(nextRow, nextColumn) && | ||
heights[row][column] <= heights[nextRow][nextColumn] && | ||
!atlantic.has(generateKey(nextRow, nextColumn)) | ||
) { | ||
queue.push([nextRow, nextColumn]); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
|
||
function isValidPosition(row, column) { | ||
if (row < 0 || ROW <= row) { | ||
return false; | ||
} | ||
if (column < 0 || COLUMN <= column) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function generateKey(row, column) { | ||
return `${row},${column}`; | ||
} | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 저도 이 문제 풀면서 O(N)으로 풀지않고 다른방법이 있을거라 생각했는데 이진탐색을 하면 시간복잡도를 줄일수 있군요!