Skip to content

Commit a911094

Browse files
authored
Merge pull request #983 from gwbaik9717/main
[ganu] Week9
2 parents aa22639 + 26c6d4f commit a911094

File tree

5 files changed

+291
-0
lines changed

5 files changed

+291
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Time complexity: O(logn)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var findMin = function (nums) {
9+
let left = 0;
10+
let right = nums.length - 1;
11+
12+
while (left < right) {
13+
const mid = Math.floor((left + right) / 2);
14+
15+
if (nums.at(mid - 1) > nums.at(mid)) {
16+
return nums[mid];
17+
}
18+
19+
if (nums.at(mid + 1) < nums.at(mid)) {
20+
return nums[mid + 1];
21+
}
22+
23+
if (nums.at(mid + 1) > nums.at(right)) {
24+
left = mid + 1;
25+
continue;
26+
}
27+
28+
right = mid - 1;
29+
}
30+
31+
return nums[left];
32+
};

linked-list-cycle/gwbaik9717.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* @param {ListNode} head
6+
* @return {boolean}
7+
*/
8+
var hasCycle = function (head) {
9+
let turtle = head;
10+
let rabbit = head;
11+
12+
while (turtle && rabbit && rabbit.next) {
13+
turtle = turtle.next;
14+
rabbit = rabbit.next.next;
15+
16+
if (turtle === rabbit) {
17+
return true;
18+
}
19+
}
20+
21+
return false;
22+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var maxProduct = function (nums) {
9+
let answer = nums[0];
10+
const products = Array.from({ length: nums.length + 1 }, () => null);
11+
products[0] = [1, 1]; // [min, max]
12+
13+
for (let i = 1; i < products.length; i++) {
14+
const cases = [];
15+
16+
// case 1
17+
cases.push(nums[i - 1]);
18+
19+
// case 2
20+
cases.push(nums[i - 1] * products[i - 1][0]);
21+
22+
// case 3
23+
cases.push(nums[i - 1] * products[i - 1][1]);
24+
25+
const product = [Math.min(...cases), Math.max(...cases)];
26+
products[i] = product;
27+
answer = Math.max(answer, product[1]);
28+
}
29+
30+
return answer;
31+
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// n: len(s), m: len(t)
2+
// Time complexity: O(n+m)
3+
// Space complexity: O(n+m)
4+
5+
/**
6+
* @param {string} s
7+
* @param {string} t
8+
* @return {string}
9+
*/
10+
var minWindow = function (s, t) {
11+
let i = 0;
12+
let j = 0;
13+
14+
const current = new Map();
15+
const target = new Map();
16+
17+
current.set(s[i], 1);
18+
19+
for (const chr of t) {
20+
if (target.has(chr)) {
21+
target.set(chr, target.get(chr) + 1);
22+
continue;
23+
}
24+
25+
target.set(chr, 1);
26+
}
27+
28+
let answer = null;
29+
30+
while (i < s.length) {
31+
let pass = true;
32+
for (const [key, value] of target) {
33+
if (!current.has(key)) {
34+
pass = false;
35+
break;
36+
}
37+
38+
if (current.get(key) < value) {
39+
pass = false;
40+
break;
41+
}
42+
}
43+
44+
if (pass) {
45+
if (!answer) {
46+
answer = s.slice(i, j + 1);
47+
}
48+
49+
if (answer && j - i + 1 < answer.length) {
50+
answer = s.slice(i, j + 1);
51+
}
52+
53+
current.set(s[i], current.get(s[i]) - 1);
54+
if (current.get(s[i]) === 0) {
55+
current.delete(s[i]);
56+
}
57+
i++;
58+
59+
continue;
60+
}
61+
62+
if (j < s.length) {
63+
j++;
64+
current.set(s[j], (current.get(s[j]) || 0) + 1);
65+
} else {
66+
break;
67+
}
68+
}
69+
70+
if (answer === null) {
71+
return "";
72+
}
73+
74+
return answer;
75+
};
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// n: height, m: width
2+
// Time complexity: O(n*m)
3+
// Space complexity: O(n*m)
4+
5+
class _Queue {
6+
constructor() {
7+
this.q = [];
8+
this.start = 0;
9+
this.end = 0;
10+
}
11+
12+
isEmpty() {
13+
return this.start === this.end;
14+
}
15+
16+
push(value) {
17+
this.q.push(value);
18+
this.end++;
19+
}
20+
21+
pop() {
22+
const rv = this.q[this.start];
23+
delete this.q[this.start++];
24+
return rv;
25+
}
26+
}
27+
/**
28+
* @param {number[][]} heights
29+
* @return {number[][]}
30+
*/
31+
var pacificAtlantic = function (heights) {
32+
const dy = [1, 0, -1, 0];
33+
const dx = [0, 1, 0, -1];
34+
35+
const h = heights.length;
36+
const w = heights[0].length;
37+
const checked = Array.from({ length: h }, () =>
38+
Array.from({ length: w }, () => [false, false])
39+
);
40+
41+
// 태평양
42+
const pacific = new _Queue();
43+
44+
for (let i = 0; i < h; i++) {
45+
if (!checked[i][0][0]) {
46+
pacific.push([i, 0]);
47+
checked[i][0][0] = true;
48+
}
49+
}
50+
51+
for (let i = 0; i < w; i++) {
52+
if (!checked[0][i][0]) {
53+
pacific.push([0, i]);
54+
checked[0][i][0] = true;
55+
}
56+
}
57+
58+
while (!pacific.isEmpty()) {
59+
const [cy, cx] = pacific.pop();
60+
61+
for (let i = 0; i < dy.length; i++) {
62+
const ny = cy + dy[i];
63+
const nx = cx + dx[i];
64+
65+
if (
66+
ny >= 0 &&
67+
ny < h &&
68+
nx >= 0 &&
69+
nx < w &&
70+
!checked[ny][nx][0] &&
71+
heights[ny][nx] >= heights[cy][cx]
72+
) {
73+
checked[ny][nx][0] = true;
74+
pacific.push([ny, nx]);
75+
}
76+
}
77+
}
78+
79+
// 대서양
80+
const answer = [];
81+
const atlantic = new _Queue();
82+
83+
for (let i = 0; i < h; i++) {
84+
if (!checked[i][w - 1][1]) {
85+
atlantic.push([i, w - 1]);
86+
checked[i][w - 1][1] = true;
87+
}
88+
89+
if (checked[i][w - 1][0] === true) {
90+
answer.push([i, w - 1]);
91+
}
92+
}
93+
94+
for (let i = 0; i < w; i++) {
95+
if (!checked[h - 1][i][1]) {
96+
atlantic.push([h - 1, i]);
97+
checked[h - 1][i][1] = true;
98+
99+
if (checked[h - 1][i][0] === true) {
100+
answer.push([h - 1, i]);
101+
}
102+
}
103+
}
104+
105+
while (!atlantic.isEmpty()) {
106+
const [cy, cx] = atlantic.pop();
107+
108+
for (let i = 0; i < dy.length; i++) {
109+
const ny = cy + dy[i];
110+
const nx = cx + dx[i];
111+
112+
if (
113+
ny >= 0 &&
114+
ny < h &&
115+
nx >= 0 &&
116+
nx < w &&
117+
!checked[ny][nx][1] &&
118+
heights[ny][nx] >= heights[cy][cx]
119+
) {
120+
if (checked[ny][nx][0] === true) {
121+
answer.push([ny, nx]);
122+
}
123+
124+
checked[ny][nx][1] = true;
125+
atlantic.push([ny, nx]);
126+
}
127+
}
128+
}
129+
130+
return answer;
131+
};

0 commit comments

Comments
 (0)