diff --git a/linked-list-cycle/hyer0705.ts b/linked-list-cycle/hyer0705.ts new file mode 100644 index 000000000..0bb12c5d3 --- /dev/null +++ b/linked-list-cycle/hyer0705.ts @@ -0,0 +1,25 @@ +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +function hasCycle(head: ListNode | null): boolean { + if (!head || !head.next) return false; + + let slow = head; + let fast = head; + while (fast && fast.next) { + slow = slow.next; + fast = fast.next.next; + if (slow === fast) return true; + } + + return false; +} diff --git a/maximum-product-subarray/hyer0705.ts b/maximum-product-subarray/hyer0705.ts new file mode 100644 index 000000000..c6fcd889f --- /dev/null +++ b/maximum-product-subarray/hyer0705.ts @@ -0,0 +1,17 @@ +function maxProduct(nums: number[]): number { + let minValue = nums[0]; + let maxValue = nums[0]; + let result = nums[0]; + + for (let i = 1; i < nums.length; i++) { + const currentNum = nums[i]; + [minValue, maxValue] = [ + Math.min(currentNum, maxValue * currentNum, minValue * currentNum), + Math.max(currentNum, maxValue * currentNum, minValue * currentNum), + ]; + + result = Math.max(result, maxValue); + } + + return result; +} diff --git a/minimum-window-substring/hyer0705.ts b/minimum-window-substring/hyer0705.ts new file mode 100644 index 000000000..f985aea92 --- /dev/null +++ b/minimum-window-substring/hyer0705.ts @@ -0,0 +1,44 @@ +function minWindow(s: string, t: string): string { + const m = s.length; + const n = t.length; + + if (m < n) return ""; + + let windowStart = 0; + let minLen = Infinity; + let substring = ""; + + let requiredChar = n; + + const countCharMap = new Map(); + for (const ch of t) { + countCharMap.set(ch, (countCharMap.get(ch) || 0) + 1); + } + + for (let windowEnd = 0; windowEnd < m; windowEnd++) { + const endChar = s[windowEnd]; + + if (countCharMap.has(endChar)) { + if (countCharMap.get(endChar)! > 0) requiredChar--; + countCharMap.set(endChar, (countCharMap.get(endChar) || 0) - 1); + } + + while (requiredChar === 0) { + const currentLen = windowEnd - windowStart + 1; + if (currentLen < minLen) { + minLen = currentLen; + substring = s.substring(windowStart, windowEnd + 1); + } + + const startChar = s[windowStart]; + windowStart++; + + if (countCharMap.has(startChar)) { + countCharMap.set(startChar, (countCharMap.get(startChar) || 0) + 1); + if (countCharMap.get(startChar)! > 0) requiredChar++; + } + } + } + + return substring; +} diff --git a/pacific-atlantic-water-flow/hyer0705.ts b/pacific-atlantic-water-flow/hyer0705.ts new file mode 100644 index 000000000..1a3615327 --- /dev/null +++ b/pacific-atlantic-water-flow/hyer0705.ts @@ -0,0 +1,54 @@ +function pacificAtlantic(heights: number[][]): number[][] { + const m = heights.length; + const n = heights[0].length; + + const directions = [ + [0, 1], + [0, -1], + [1, 0], + [-1, 0], + ]; + + const helper = (row: number, col: number): boolean[][] => { + const canReach: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false)); + + const queue: [number, number][] = []; + + for (let i = 0; i < n; i++) { + canReach[row][i] = true; + queue.push([row, i]); + } + for (let i = 0; i < m; i++) { + canReach[i][col] = true; + queue.push([i, col]); + } + + while (queue.length > 0) { + const [cx, cy] = queue.shift()!; + + for (const [dx, dy] of directions) { + const [nx, ny] = [cx + dx, cy + dy]; + + if (nx >= 0 && nx < m && ny >= 0 && ny < n && !canReach[nx][ny] && heights[cx][cy] <= heights[nx][ny]) { + canReach[nx][ny] = true; + queue.push([nx, ny]); + } + } + } + + return canReach; + }; + + const canReachPacific: boolean[][] = helper(0, 0); + const canReachAtlantic: boolean[][] = helper(m - 1, n - 1); + + const results: number[][] = []; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (canReachPacific[i][j] && canReachAtlantic[i][j]) results.push([i, j]); + } + } + + return results; +} diff --git a/sum-of-two-integers/hyer0705.ts b/sum-of-two-integers/hyer0705.ts new file mode 100644 index 000000000..43ae21477 --- /dev/null +++ b/sum-of-two-integers/hyer0705.ts @@ -0,0 +1,10 @@ +function getSum(a: number, b: number): number { + while (b !== 0) { + let carry = (a & b) << 1; + + a = a ^ b; + + b = carry; + } + return a; +}