Skip to content

Commit e370998

Browse files
authored
Merge pull request #1535 from hsskey/main
[hsskey] WEEK 09 solutions
2 parents 832329a + 361d527 commit e370998

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

linked-list-cycle/hsskey.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {ListNode} head
11+
* @return {boolean}
12+
*/
13+
var hasCycle = function(head) {
14+
let slow = head;
15+
let fast = head;
16+
17+
while (fast !== null && fast.next !== null) {
18+
slow = slow.next;
19+
fast = fast.next.next;
20+
21+
if (slow === fast) {
22+
return true;
23+
}
24+
}
25+
26+
return false;
27+
};

maximum-product-subarray/hsskey.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxProduct = function(nums) {
6+
let res = Math.max(...nums);
7+
let curMin = 1;
8+
let curMax = 1;
9+
10+
for (let n of nums) {
11+
if (n === 0) {
12+
curMin = 1;
13+
curMax = 1;
14+
continue;
15+
}
16+
17+
let tmp = curMax * n;
18+
curMax = Math.max(n * curMax, n * curMin, n);
19+
curMin = Math.min(tmp, n * curMin, n);
20+
21+
res = Math.max(res, curMax);
22+
}
23+
24+
return res;
25+
};

minimum-window-substring/hsskey.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {string}
5+
*/
6+
var minWindow = function(s, t) {
7+
if (s.length === 0 || t.length === 0) return "";
8+
9+
const dictT = {};
10+
for (let char of t) {
11+
dictT[char] = (dictT[char] || 0) + 1;
12+
}
13+
14+
const required = Object.keys(dictT).length;
15+
let formed = 0;
16+
17+
const windowCounts = {};
18+
19+
let left = 0, right = 0;
20+
21+
let minLen = Infinity;
22+
let minLeft = 0, minRight = 0;
23+
24+
while (right < s.length) {
25+
const character = s[right];
26+
windowCounts[character] = (windowCounts[character] || 0) + 1;
27+
28+
if (dictT[character] && windowCounts[character] === dictT[character]) {
29+
formed++;
30+
}
31+
32+
while (left <= right && formed === required) {
33+
const currentLen = right - left + 1;
34+
35+
if (currentLen < minLen) {
36+
minLen = currentLen;
37+
minLeft = left;
38+
minRight = right;
39+
}
40+
41+
const leftChar = s[left];
42+
windowCounts[leftChar]--;
43+
44+
if (dictT[leftChar] && windowCounts[leftChar] < dictT[leftChar]) {
45+
formed--;
46+
}
47+
48+
left++;
49+
}
50+
51+
right++;
52+
}
53+
54+
return minLen === Infinity ? "" : s.substring(minLeft, minRight + 1);
55+
};

pacific-atlantic-water-flow/hsskey.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @param {number[][]} heights
3+
* @return {number[][]}
4+
*/
5+
var pacificAtlantic = function(heights) {
6+
const ROWS = heights.length;
7+
const COLS = heights[0].length;
8+
9+
const pac = new Set();
10+
const atl = new Set();
11+
12+
const dfs = (r, c, visit, prevHeight) => {
13+
const key = `${r},${c}`;
14+
if (
15+
visit.has(key) ||
16+
r < 0 || c < 0 || r >= ROWS || c >= COLS ||
17+
heights[r][c] < prevHeight
18+
) {
19+
return;
20+
}
21+
22+
visit.add(key);
23+
24+
dfs(r + 1, c, visit, heights[r][c]);
25+
dfs(r - 1, c, visit, heights[r][c]);
26+
dfs(r, c + 1, visit, heights[r][c]);
27+
dfs(r, c - 1, visit, heights[r][c]);
28+
};
29+
30+
for (let c = 0; c < COLS; c++) {
31+
dfs(0, c, pac, heights[0][c]); // Top row (Pacific)
32+
dfs(ROWS - 1, c, atl, heights[ROWS - 1][c]); // Bottom row (Atlantic)
33+
}
34+
35+
for (let r = 0; r < ROWS; r++) {
36+
dfs(r, 0, pac, heights[r][0]); // Left col (Pacific)
37+
dfs(r, COLS - 1, atl, heights[r][COLS - 1]); // Right col (Atlantic)
38+
}
39+
40+
const res = [];
41+
for (let r = 0; r < ROWS; r++) {
42+
for (let c = 0; c < COLS; c++) {
43+
const key = `${r},${c}`;
44+
if (pac.has(key) && atl.has(key)) {
45+
res.push([r, c]);
46+
}
47+
}
48+
}
49+
50+
return res;
51+
};

sum-of-two-integers/hsskey.js

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

0 commit comments

Comments
 (0)