From 3481e875c0a724f76d9b9fefce74ea7207328329 Mon Sep 17 00:00:00 2001 From: ganu Date: Sun, 2 Feb 2025 11:51:13 +0900 Subject: [PATCH 1/6] feat: 141. Linked List Cycle --- linked-list-cycle/gwbaik9717.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 linked-list-cycle/gwbaik9717.js diff --git a/linked-list-cycle/gwbaik9717.js b/linked-list-cycle/gwbaik9717.js new file mode 100644 index 000000000..1d0f48b92 --- /dev/null +++ b/linked-list-cycle/gwbaik9717.js @@ -0,0 +1,22 @@ +// Time complexity: O(n) +// Space complexity: O(1) + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function (head) { + let turtle = head; + let rabbit = head; + + while (turtle && rabbit && rabbit.next) { + turtle = turtle.next; + rabbit = rabbit.next.next; + + if (turtle === rabbit) { + return true; + } + } + + return false; +}; From 3276ce15e2df18a77e5e255403dc0529fce03c06 Mon Sep 17 00:00:00 2001 From: ganu Date: Thu, 6 Feb 2025 06:27:21 +0900 Subject: [PATCH 2/6] feat: 152. Maximum Product Subarray --- maximum-product-subarray/gwbaik9717.js | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 maximum-product-subarray/gwbaik9717.js diff --git a/maximum-product-subarray/gwbaik9717.js b/maximum-product-subarray/gwbaik9717.js new file mode 100644 index 000000000..66f910bfd --- /dev/null +++ b/maximum-product-subarray/gwbaik9717.js @@ -0,0 +1,39 @@ +// Time complexity: O(n^2) +// Space complexity: O(n) + +/** + * @param {number[]} nums + * @return {number} + */ +var maxProduct = function (nums) { + let answer = nums[0]; + + const productGroups = [[]]; + + for (let i = 0; i < nums.length; i++) { + const productGroup = productGroups.at(-1); + + if (nums[i] === 0) { + productGroups.push([]); + } + + if (productGroup.length === 0) { + productGroup.push(nums[i]); + continue; + } + + productGroup.push(nums[i] * productGroup.at(-1)); + } + + for (const group of productGroups) { + for (let i = 0; i < group.length; i++) { + answer = Math.max(answer, group[i]); + + for (let j = 0; j < i; j++) { + answer = Math.max(answer, group[i] / group[j]); + } + } + } + + return answer; +}; From abf4820a04a8ab3c39580ea5827576a02b8d34c5 Mon Sep 17 00:00:00 2001 From: ganu Date: Thu, 6 Feb 2025 21:23:57 +0900 Subject: [PATCH 3/6] refactor: feat: 152. Maximum Product Subarray --- maximum-product-subarray/gwbaik9717.js | 36 ++++++++++---------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/maximum-product-subarray/gwbaik9717.js b/maximum-product-subarray/gwbaik9717.js index 66f910bfd..6b228c635 100644 --- a/maximum-product-subarray/gwbaik9717.js +++ b/maximum-product-subarray/gwbaik9717.js @@ -1,4 +1,4 @@ -// Time complexity: O(n^2) +// Time complexity: O(n) // Space complexity: O(n) /** @@ -7,32 +7,24 @@ */ var maxProduct = function (nums) { let answer = nums[0]; + const products = Array.from({ length: nums.length + 1 }, () => null); + products[0] = [1, 1]; // [min, max] - const productGroups = [[]]; + for (let i = 1; i < products.length; i++) { + const cases = []; - for (let i = 0; i < nums.length; i++) { - const productGroup = productGroups.at(-1); + // case 1 + cases.push(nums[i - 1]); - if (nums[i] === 0) { - productGroups.push([]); - } + // case 2 + cases.push(nums[i - 1] * products[i - 1][0]); - if (productGroup.length === 0) { - productGroup.push(nums[i]); - continue; - } + // case 3 + cases.push(nums[i - 1] * products[i - 1][1]); - productGroup.push(nums[i] * productGroup.at(-1)); - } - - for (const group of productGroups) { - for (let i = 0; i < group.length; i++) { - answer = Math.max(answer, group[i]); - - for (let j = 0; j < i; j++) { - answer = Math.max(answer, group[i] / group[j]); - } - } + const product = [Math.min(...cases), Math.max(...cases)]; + products[i] = product; + answer = Math.max(answer, product[1]); } return answer; From 2bd54e9764f6df3e65482f9b45612d443e2bf30c Mon Sep 17 00:00:00 2001 From: ganu Date: Fri, 7 Feb 2025 06:21:18 +0900 Subject: [PATCH 4/6] feat: 153. Find Minimum in Rotated Sorted Array --- .../gwbaik9717.js | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/gwbaik9717.js diff --git a/find-minimum-in-rotated-sorted-array/gwbaik9717.js b/find-minimum-in-rotated-sorted-array/gwbaik9717.js new file mode 100644 index 000000000..f42181c24 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/gwbaik9717.js @@ -0,0 +1,32 @@ +// Time complexity: O(logn) +// Space complexity: O(1) + +/** + * @param {number[]} nums + * @return {number} + */ +var findMin = function (nums) { + let left = 0; + let right = nums.length - 1; + + while (left < right) { + const mid = Math.floor((left + right) / 2); + + if (nums.at(mid - 1) > nums.at(mid)) { + return nums[mid]; + } + + if (nums.at(mid + 1) < nums.at(mid)) { + return nums[mid + 1]; + } + + if (nums.at(mid + 1) > nums.at(right)) { + left = mid + 1; + continue; + } + + right = mid - 1; + } + + return nums[left]; +}; From 0cfc687bf266af50b2180b6ae57306744da88274 Mon Sep 17 00:00:00 2001 From: ganu Date: Sat, 8 Feb 2025 10:37:18 +0900 Subject: [PATCH 5/6] feat: 417. Pacific Atlantic Water Flow --- pacific-atlantic-water-flow/gwbaik9717.js | 131 ++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 pacific-atlantic-water-flow/gwbaik9717.js diff --git a/pacific-atlantic-water-flow/gwbaik9717.js b/pacific-atlantic-water-flow/gwbaik9717.js new file mode 100644 index 000000000..8195d7f1a --- /dev/null +++ b/pacific-atlantic-water-flow/gwbaik9717.js @@ -0,0 +1,131 @@ +// n: height, m: width +// Time complexity: O(n*m) +// Space complexity: O(n*m) + +class _Queue { + constructor() { + this.q = []; + this.start = 0; + this.end = 0; + } + + isEmpty() { + return this.start === this.end; + } + + push(value) { + this.q.push(value); + this.end++; + } + + pop() { + const rv = this.q[this.start]; + delete this.q[this.start++]; + return rv; + } +} +/** + * @param {number[][]} heights + * @return {number[][]} + */ +var pacificAtlantic = function (heights) { + const dy = [1, 0, -1, 0]; + const dx = [0, 1, 0, -1]; + + const h = heights.length; + const w = heights[0].length; + const checked = Array.from({ length: h }, () => + Array.from({ length: w }, () => [false, false]) + ); + + // 태평양 + const pacific = new _Queue(); + + for (let i = 0; i < h; i++) { + if (!checked[i][0][0]) { + pacific.push([i, 0]); + checked[i][0][0] = true; + } + } + + for (let i = 0; i < w; i++) { + if (!checked[0][i][0]) { + pacific.push([0, i]); + checked[0][i][0] = true; + } + } + + while (!pacific.isEmpty()) { + const [cy, cx] = pacific.pop(); + + for (let i = 0; i < dy.length; i++) { + const ny = cy + dy[i]; + const nx = cx + dx[i]; + + if ( + ny >= 0 && + ny < h && + nx >= 0 && + nx < w && + !checked[ny][nx][0] && + heights[ny][nx] >= heights[cy][cx] + ) { + checked[ny][nx][0] = true; + pacific.push([ny, nx]); + } + } + } + + // 대서양 + const answer = []; + const atlantic = new _Queue(); + + for (let i = 0; i < h; i++) { + if (!checked[i][w - 1][1]) { + atlantic.push([i, w - 1]); + checked[i][w - 1][1] = true; + } + + if (checked[i][w - 1][0] === true) { + answer.push([i, w - 1]); + } + } + + for (let i = 0; i < w; i++) { + if (!checked[h - 1][i][1]) { + atlantic.push([h - 1, i]); + checked[h - 1][i][1] = true; + + if (checked[h - 1][i][0] === true) { + answer.push([h - 1, i]); + } + } + } + + while (!atlantic.isEmpty()) { + const [cy, cx] = atlantic.pop(); + + for (let i = 0; i < dy.length; i++) { + const ny = cy + dy[i]; + const nx = cx + dx[i]; + + if ( + ny >= 0 && + ny < h && + nx >= 0 && + nx < w && + !checked[ny][nx][1] && + heights[ny][nx] >= heights[cy][cx] + ) { + if (checked[ny][nx][0] === true) { + answer.push([ny, nx]); + } + + checked[ny][nx][1] = true; + atlantic.push([ny, nx]); + } + } + } + + return answer; +}; From 26c6d4f4d8056eafed48446fb1998da21203ab7b Mon Sep 17 00:00:00 2001 From: ganu Date: Sat, 8 Feb 2025 11:39:48 +0900 Subject: [PATCH 6/6] feat: 76. Minimum Window Substring --- minimum-window-substring/gwbaik9717.js | 75 ++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 minimum-window-substring/gwbaik9717.js diff --git a/minimum-window-substring/gwbaik9717.js b/minimum-window-substring/gwbaik9717.js new file mode 100644 index 000000000..4f2dfc87d --- /dev/null +++ b/minimum-window-substring/gwbaik9717.js @@ -0,0 +1,75 @@ +// n: len(s), m: len(t) +// Time complexity: O(n+m) +// Space complexity: O(n+m) + +/** + * @param {string} s + * @param {string} t + * @return {string} + */ +var minWindow = function (s, t) { + let i = 0; + let j = 0; + + const current = new Map(); + const target = new Map(); + + current.set(s[i], 1); + + for (const chr of t) { + if (target.has(chr)) { + target.set(chr, target.get(chr) + 1); + continue; + } + + target.set(chr, 1); + } + + let answer = null; + + while (i < s.length) { + let pass = true; + for (const [key, value] of target) { + if (!current.has(key)) { + pass = false; + break; + } + + if (current.get(key) < value) { + pass = false; + break; + } + } + + if (pass) { + if (!answer) { + answer = s.slice(i, j + 1); + } + + if (answer && j - i + 1 < answer.length) { + answer = s.slice(i, j + 1); + } + + current.set(s[i], current.get(s[i]) - 1); + if (current.get(s[i]) === 0) { + current.delete(s[i]); + } + i++; + + continue; + } + + if (j < s.length) { + j++; + current.set(s[j], (current.get(s[j]) || 0) + 1); + } else { + break; + } + } + + if (answer === null) { + return ""; + } + + return answer; +};