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 09 Solutions #516

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions find-minimum-in-rotated-sorted-array/wogha95.js
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];
};
36 changes: 36 additions & 0 deletions linked-list-cycle/wogha95.js
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;
};
24 changes: 24 additions & 0 deletions maximum-subarray/wogha95.js
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;
};
94 changes: 94 additions & 0 deletions pacific-atlantic-water-flow/wogha95.js
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}`;
}
};