Skip to content

Commit 0b457eb

Browse files
authored
Merge pull request #515 from Sunjae95/main
[선재] WEEK09 Solution
2 parents 359ae2c + 8621430 commit 0b457eb

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* brute force
5+
*
6+
* n = length of head
7+
* time complexity: O(n)
8+
* space complexity: O(1)
9+
*/
10+
var findMin = function (nums) {
11+
let answer = 5000;
12+
nums.forEach((num) => (answer = Math.min(answer, num)));
13+
14+
return answer;
15+
};
16+
17+
/* n = length of head
18+
* time complexity: O(n)
19+
* space complexity: O(1)
20+
*/
21+
var findMin = function (nums) {
22+
let answer = nums[0];
23+
if (nums.length === 1) return answer;
24+
if (answer < nums[nums.length - 1]) return answer;
25+
26+
for (let i = nums.length - 1; i >= 0; i--) {
27+
if (answer < nums[i]) return answer;
28+
answer = nums[i];
29+
}
30+
31+
return answer;
32+
};

linked-list-cycle/sunjae95.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* hash table
5+
*
6+
* n = length of head
7+
* time complexity: O(n)
8+
* space complexity: O(n)
9+
*/
10+
var hasCycle = function (head) {
11+
const set = new Set();
12+
let node = head;
13+
14+
while (node) {
15+
if (set.has(node)) return true;
16+
set.add(node);
17+
node = node.next;
18+
}
19+
20+
return false;
21+
};

maximum-subarray/sunjae95.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @description
3+
* n = length of nums
4+
* time complexity: O(n)
5+
* space complexity: O(1)
6+
*/
7+
var maxSubArray = function (nums) {
8+
let sum = 0;
9+
let answer = nums[0];
10+
11+
for (let end = 0; end < nums.length; end++) {
12+
if (sum < 0) {
13+
sum = nums[end];
14+
} else if (sum + nums[end] >= 0) {
15+
sum += nums[end];
16+
} else {
17+
sum = nums[end];
18+
}
19+
20+
answer = Math.max(answer, sum);
21+
}
22+
return answer;
23+
};

minimum-window-substring/sunjae.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* two pointer + hash table
5+
*
6+
* n = length of s
7+
* m = length of t
8+
* time complexity: O(n*n)
9+
* space complexity: O(n)
10+
*/
11+
var minWindow = function (s, t) {
12+
const requiredMap = t.split("").reduce((map, char) => {
13+
map.set(char, (map.get(char) ?? 0) + 1);
14+
return map;
15+
}, new Map());
16+
const requiredArray = [...requiredMap.entries()];
17+
const map = new Map();
18+
const successRequired = () =>
19+
requiredArray.every(([key, value]) => map.get(key) >= value);
20+
21+
let answer = "";
22+
let start = 0;
23+
24+
for (let i = 0; i < s.length; i++) {
25+
if (requiredMap.has(s[i])) map.set(s[i], (map.get(s[i]) ?? 0) + 1);
26+
27+
while (successRequired()) {
28+
const now = s.slice(start, i + 1);
29+
answer = answer === "" || answer.length >= now.length ? now : answer;
30+
31+
if (map.has(s[start])) {
32+
map.set(s[start], map.get(s[start]) - 1);
33+
if (map.get(s[start]) === -1) map.delete(s[start]);
34+
}
35+
36+
start++;
37+
}
38+
}
39+
40+
return answer;
41+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* bfs + memoization
5+
*
6+
* n = length of height
7+
* m = length of height[i]
8+
* time complexity: O(n*m * 4^n*m)
9+
* space complexity: O(n*m)
10+
*/
11+
var pacificAtlantic = function (heights) {
12+
const visited = Array.from({ length: heights.length }, (_, i) =>
13+
Array.from({ length: heights[i].length }, () => false)
14+
);
15+
const memo = new Map();
16+
17+
const bfs = (row, column) => {
18+
const dr = [0, 0, -1, 1];
19+
const dc = [1, -1, 0, 0];
20+
const queue = [[row, column]];
21+
let [pacific, atlantic] = [false, false];
22+
let queueIndex = 0;
23+
24+
while (queueIndex !== queue.length) {
25+
const currentLastLength = queue.length;
26+
27+
while (queueIndex !== currentLastLength) {
28+
const [r, c] = queue[queueIndex++];
29+
visited[r][c] = `${row},${column}`;
30+
31+
if (memo.has(`${r},${c}`)) {
32+
memo.set(`${row},${column}`, [row, column]);
33+
return;
34+
}
35+
36+
for (let i = 0; i < 4; i++) {
37+
const nextR = r + dr[i];
38+
const nextC = c + dc[i];
39+
const isPacific = nextR === -1 || nextC === -1;
40+
const isAtlantic =
41+
nextR === heights.length || nextC === heights[0].length;
42+
43+
if (isPacific) {
44+
pacific = true;
45+
continue;
46+
}
47+
if (isAtlantic) {
48+
atlantic = true;
49+
continue;
50+
}
51+
52+
if (visited[nextR][nextC] === `${row},${column}`) continue;
53+
54+
if (heights[r][c] < heights[nextR][nextC]) continue;
55+
56+
queue.push([nextR, nextC]);
57+
}
58+
}
59+
60+
if (pacific && atlantic) {
61+
memo.set(`${row},${column}`, [row, column]);
62+
return;
63+
}
64+
}
65+
};
66+
67+
for (let row = 0; row < heights.length; row++) {
68+
for (let column = 0; column < heights[row].length; column++) {
69+
bfs(row, column);
70+
}
71+
}
72+
73+
return [...memo.values()];
74+
};

0 commit comments

Comments
 (0)