From 659bf6ef95e35c51f416498dc30e55bc30cf9f54 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sun, 6 Oct 2024 23:52:18 +0900 Subject: [PATCH 1/5] solve: linked list cycle --- linked-list-cycle/wogha95.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 linked-list-cycle/wogha95.js diff --git a/linked-list-cycle/wogha95.js b/linked-list-cycle/wogha95.js new file mode 100644 index 00000000..d1b65f66 --- /dev/null +++ b/linked-list-cycle/wogha95.js @@ -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; +}; From edb839887294a082aefd7cec1ab1addce659c2e2 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Tue, 8 Oct 2024 23:04:03 +0900 Subject: [PATCH 2/5] solve: find minimum in rotated sorted array --- .../wogha95.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/wogha95.js diff --git a/find-minimum-in-rotated-sorted-array/wogha95.js b/find-minimum-in-rotated-sorted-array/wogha95.js new file mode 100644 index 00000000..950620af --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/wogha95.js @@ -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]; +}; From 73ea4ab7059f2b4411f1a54625fd8b28bf32385c Mon Sep 17 00:00:00 2001 From: wogha95 Date: Wed, 9 Oct 2024 19:01:09 +0900 Subject: [PATCH 3/5] solve: find minimum in rotated sorted array --- find-minimum-in-rotated-sorted-array/wogha95.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/find-minimum-in-rotated-sorted-array/wogha95.js b/find-minimum-in-rotated-sorted-array/wogha95.js index 950620af..c1ca60e1 100644 --- a/find-minimum-in-rotated-sorted-array/wogha95.js +++ b/find-minimum-in-rotated-sorted-array/wogha95.js @@ -1,6 +1,6 @@ /** * TC: O(log N) - * 이진탐색으로 log N으로 계산 + * 문제에 명시된 log N 복잡도를 갖기위해 이진탐색 * * SC: O(1) */ From 1657c59cfc955de8ff3b763b202e2698b4abe417 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Wed, 9 Oct 2024 19:11:34 +0900 Subject: [PATCH 4/5] solve: pacific atlantic water flow --- pacific-atlantic-water-flow/wogha95.js | 94 ++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 pacific-atlantic-water-flow/wogha95.js diff --git a/pacific-atlantic-water-flow/wogha95.js b/pacific-atlantic-water-flow/wogha95.js new file mode 100644 index 00000000..7d73db7b --- /dev/null +++ b/pacific-atlantic-water-flow/wogha95.js @@ -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}`; + } +}; From 06b49b974cbbfb923ca8620f02ff4d64c04188c5 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Wed, 9 Oct 2024 19:43:54 +0900 Subject: [PATCH 5/5] solve: maximum subarray --- maximum-subarray/wogha95.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 maximum-subarray/wogha95.js diff --git a/maximum-subarray/wogha95.js b/maximum-subarray/wogha95.js new file mode 100644 index 00000000..8b1e450b --- /dev/null +++ b/maximum-subarray/wogha95.js @@ -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; +};