Skip to content

Commit 1ba4be0

Browse files
authored
Merge pull request #1908 from hyer0705/main
2 parents 65a41cd + 139e6c1 commit 1ba4be0

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

linked-list-cycle/hyer0705.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function hasCycle(head: ListNode | null): boolean {
14+
if (!head || !head.next) return false;
15+
16+
let slow = head;
17+
let fast = head;
18+
while (fast && fast.next) {
19+
slow = slow.next;
20+
fast = fast.next.next;
21+
if (slow === fast) return true;
22+
}
23+
24+
return false;
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function maxProduct(nums: number[]): number {
2+
let minValue = nums[0];
3+
let maxValue = nums[0];
4+
let result = nums[0];
5+
6+
for (let i = 1; i < nums.length; i++) {
7+
const currentNum = nums[i];
8+
[minValue, maxValue] = [
9+
Math.min(currentNum, maxValue * currentNum, minValue * currentNum),
10+
Math.max(currentNum, maxValue * currentNum, minValue * currentNum),
11+
];
12+
13+
result = Math.max(result, maxValue);
14+
}
15+
16+
return result;
17+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function minWindow(s: string, t: string): string {
2+
const m = s.length;
3+
const n = t.length;
4+
5+
if (m < n) return "";
6+
7+
let windowStart = 0;
8+
let minLen = Infinity;
9+
let substring = "";
10+
11+
let requiredChar = n;
12+
13+
const countCharMap = new Map<string, number>();
14+
for (const ch of t) {
15+
countCharMap.set(ch, (countCharMap.get(ch) || 0) + 1);
16+
}
17+
18+
for (let windowEnd = 0; windowEnd < m; windowEnd++) {
19+
const endChar = s[windowEnd];
20+
21+
if (countCharMap.has(endChar)) {
22+
if (countCharMap.get(endChar)! > 0) requiredChar--;
23+
countCharMap.set(endChar, (countCharMap.get(endChar) || 0) - 1);
24+
}
25+
26+
while (requiredChar === 0) {
27+
const currentLen = windowEnd - windowStart + 1;
28+
if (currentLen < minLen) {
29+
minLen = currentLen;
30+
substring = s.substring(windowStart, windowEnd + 1);
31+
}
32+
33+
const startChar = s[windowStart];
34+
windowStart++;
35+
36+
if (countCharMap.has(startChar)) {
37+
countCharMap.set(startChar, (countCharMap.get(startChar) || 0) + 1);
38+
if (countCharMap.get(startChar)! > 0) requiredChar++;
39+
}
40+
}
41+
}
42+
43+
return substring;
44+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function pacificAtlantic(heights: number[][]): number[][] {
2+
const m = heights.length;
3+
const n = heights[0].length;
4+
5+
const directions = [
6+
[0, 1],
7+
[0, -1],
8+
[1, 0],
9+
[-1, 0],
10+
];
11+
12+
const helper = (row: number, col: number): boolean[][] => {
13+
const canReach: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false));
14+
15+
const queue: [number, number][] = [];
16+
17+
for (let i = 0; i < n; i++) {
18+
canReach[row][i] = true;
19+
queue.push([row, i]);
20+
}
21+
for (let i = 0; i < m; i++) {
22+
canReach[i][col] = true;
23+
queue.push([i, col]);
24+
}
25+
26+
while (queue.length > 0) {
27+
const [cx, cy] = queue.shift()!;
28+
29+
for (const [dx, dy] of directions) {
30+
const [nx, ny] = [cx + dx, cy + dy];
31+
32+
if (nx >= 0 && nx < m && ny >= 0 && ny < n && !canReach[nx][ny] && heights[cx][cy] <= heights[nx][ny]) {
33+
canReach[nx][ny] = true;
34+
queue.push([nx, ny]);
35+
}
36+
}
37+
}
38+
39+
return canReach;
40+
};
41+
42+
const canReachPacific: boolean[][] = helper(0, 0);
43+
const canReachAtlantic: boolean[][] = helper(m - 1, n - 1);
44+
45+
const results: number[][] = [];
46+
47+
for (let i = 0; i < m; i++) {
48+
for (let j = 0; j < n; j++) {
49+
if (canReachPacific[i][j] && canReachAtlantic[i][j]) results.push([i, j]);
50+
}
51+
}
52+
53+
return results;
54+
}

sum-of-two-integers/hyer0705.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function getSum(a: number, b: number): number {
2+
while (b !== 0) {
3+
let carry = (a & b) << 1;
4+
5+
a = a ^ b;
6+
7+
b = carry;
8+
}
9+
return a;
10+
}

0 commit comments

Comments
 (0)