-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #516 from wogha95/main
[재호] WEEK 09 Solutions
- Loading branch information
Showing
4 changed files
with
191 additions
and
0 deletions.
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 알고리즘을 바탕으로 | ||
* 한칸씩 이동하는 포인터와 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}`; | ||
} | ||
}; |