diff --git a/linked-list-cycle/hsskey.js b/linked-list-cycle/hsskey.js new file mode 100644 index 000000000..0dacc476b --- /dev/null +++ b/linked-list-cycle/hsskey.js @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function(head) { + let slow = head; + let fast = head; + + while (fast !== null && fast.next !== null) { + slow = slow.next; + fast = fast.next.next; + + if (slow === fast) { + return true; + } + } + + return false; +}; diff --git a/maximum-product-subarray/hsskey.js b/maximum-product-subarray/hsskey.js new file mode 100644 index 000000000..fe07f1fe7 --- /dev/null +++ b/maximum-product-subarray/hsskey.js @@ -0,0 +1,25 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var maxProduct = function(nums) { + let res = Math.max(...nums); + let curMin = 1; + let curMax = 1; + + for (let n of nums) { + if (n === 0) { + curMin = 1; + curMax = 1; + continue; + } + + let tmp = curMax * n; + curMax = Math.max(n * curMax, n * curMin, n); + curMin = Math.min(tmp, n * curMin, n); + + res = Math.max(res, curMax); + } + + return res; +}; diff --git a/minimum-window-substring/hsskey.js b/minimum-window-substring/hsskey.js new file mode 100644 index 000000000..1c0e60169 --- /dev/null +++ b/minimum-window-substring/hsskey.js @@ -0,0 +1,55 @@ +/** + * @param {string} s + * @param {string} t + * @return {string} + */ +var minWindow = function(s, t) { + if (s.length === 0 || t.length === 0) return ""; + + const dictT = {}; + for (let char of t) { + dictT[char] = (dictT[char] || 0) + 1; + } + + const required = Object.keys(dictT).length; + let formed = 0; + + const windowCounts = {}; + + let left = 0, right = 0; + + let minLen = Infinity; + let minLeft = 0, minRight = 0; + + while (right < s.length) { + const character = s[right]; + windowCounts[character] = (windowCounts[character] || 0) + 1; + + if (dictT[character] && windowCounts[character] === dictT[character]) { + formed++; + } + + while (left <= right && formed === required) { + const currentLen = right - left + 1; + + if (currentLen < minLen) { + minLen = currentLen; + minLeft = left; + minRight = right; + } + + const leftChar = s[left]; + windowCounts[leftChar]--; + + if (dictT[leftChar] && windowCounts[leftChar] < dictT[leftChar]) { + formed--; + } + + left++; + } + + right++; + } + + return minLen === Infinity ? "" : s.substring(minLeft, minRight + 1); +}; diff --git a/pacific-atlantic-water-flow/hsskey.js b/pacific-atlantic-water-flow/hsskey.js new file mode 100644 index 000000000..ad0eef8ab --- /dev/null +++ b/pacific-atlantic-water-flow/hsskey.js @@ -0,0 +1,51 @@ +/** + * @param {number[][]} heights + * @return {number[][]} + */ +var pacificAtlantic = function(heights) { + const ROWS = heights.length; + const COLS = heights[0].length; + + const pac = new Set(); + const atl = new Set(); + + const dfs = (r, c, visit, prevHeight) => { + const key = `${r},${c}`; + if ( + visit.has(key) || + r < 0 || c < 0 || r >= ROWS || c >= COLS || + heights[r][c] < prevHeight + ) { + return; + } + + visit.add(key); + + dfs(r + 1, c, visit, heights[r][c]); + dfs(r - 1, c, visit, heights[r][c]); + dfs(r, c + 1, visit, heights[r][c]); + dfs(r, c - 1, visit, heights[r][c]); + }; + + for (let c = 0; c < COLS; c++) { + dfs(0, c, pac, heights[0][c]); // Top row (Pacific) + dfs(ROWS - 1, c, atl, heights[ROWS - 1][c]); // Bottom row (Atlantic) + } + + for (let r = 0; r < ROWS; r++) { + dfs(r, 0, pac, heights[r][0]); // Left col (Pacific) + dfs(r, COLS - 1, atl, heights[r][COLS - 1]); // Right col (Atlantic) + } + + const res = []; + for (let r = 0; r < ROWS; r++) { + for (let c = 0; c < COLS; c++) { + const key = `${r},${c}`; + if (pac.has(key) && atl.has(key)) { + res.push([r, c]); + } + } + } + + return res; +}; diff --git a/sum-of-two-integers/hsskey.js b/sum-of-two-integers/hsskey.js new file mode 100644 index 000000000..13ac99cf1 --- /dev/null +++ b/sum-of-two-integers/hsskey.js @@ -0,0 +1,14 @@ +/** + * @param {number} a + * @param {number} b + * @return {number} + */ +var getSum = function(a, b) { + while (b !== 0) { + let carry = (a & b) << 1; + a = a ^ b; + b = carry; + } + return a; +}; +