Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions linked-list-cycle/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -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;
}
17 changes: 17 additions & 0 deletions maximum-product-subarray/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -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;
}
44 changes: 44 additions & 0 deletions minimum-window-substring/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -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<string, number>();
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;
}
54 changes: 54 additions & 0 deletions pacific-atlantic-water-flow/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions sum-of-two-integers/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -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;
}