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]; +}; 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; +}; diff --git a/maximum-product-subarray/gwbaik9717.js b/maximum-product-subarray/gwbaik9717.js new file mode 100644 index 000000000..6b228c635 --- /dev/null +++ b/maximum-product-subarray/gwbaik9717.js @@ -0,0 +1,31 @@ +// Time complexity: O(n) +// Space complexity: O(n) + +/** + * @param {number[]} nums + * @return {number} + */ +var maxProduct = function (nums) { + let answer = nums[0]; + const products = Array.from({ length: nums.length + 1 }, () => null); + products[0] = [1, 1]; // [min, max] + + for (let i = 1; i < products.length; i++) { + const cases = []; + + // case 1 + cases.push(nums[i - 1]); + + // case 2 + cases.push(nums[i - 1] * products[i - 1][0]); + + // case 3 + cases.push(nums[i - 1] * products[i - 1][1]); + + const product = [Math.min(...cases), Math.max(...cases)]; + products[i] = product; + answer = Math.max(answer, product[1]); + } + + return answer; +}; 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; +}; 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; +};