Skip to content

Commit 9fe16f7

Browse files
authored
Merge pull request #1522 from uraflower/main
[uraflower] Week 09 Solutions
2 parents af257ad + 3ca602b commit 9fe16f7

File tree

5 files changed

+211
-0
lines changed

5 files changed

+211
-0
lines changed

โ€Žlinked-list-cycle/uraflower.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
* ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์‚ฌ์ดํด ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
11+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
12+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
13+
* @param {ListNode} head
14+
* @return {boolean}
15+
*/
16+
const hasCycle = function(head) {
17+
let node = head;
18+
19+
while (node?.next) {
20+
if (node.val === null) {
21+
return true;
22+
}
23+
node.val = null;
24+
node = node.next;
25+
}
26+
27+
return false;
28+
};
29+
30+
// ๊ฑฐ๋ถ์ด์™€ ํ† ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜
31+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
32+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
33+
const hasCycle = function(head) {
34+
let slow = head; // ํ•œ ๋ฒˆ์— ํ•œ ๋…ธ๋“œ์”ฉ
35+
let fast = head; // ํ•œ ๋ฒˆ์— ๋‘ ๋…ธ๋“œ์”ฉ
36+
37+
while (fast?.next) {
38+
slow = slow.next;
39+
fast = fast.next.next;
40+
41+
// ํ•œ ๋ฒˆ์— ๋‘ ๋…ธ๋“œ์”ฉ ๊ฐ€๋Š” fast๊ฐ€
42+
// ์ž์‹ ๋ณด๋‹ค ๋А๋ฆฐ slow๋ž‘ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธด๋‹ค๋ฉด
43+
// ์‚ฌ์ดํด์ด ์žˆ๋‹ค๋Š” ๋œป
44+
if (slow === fast) {
45+
return true;
46+
}
47+
}
48+
49+
return false;
50+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ๊ณฑ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
const maxProduct = function (nums) {
7+
let min = nums[0];
8+
let max = nums[0];
9+
let result = nums[0]; // ํ˜„์žฌ๊นŒ์ง€ ๊ฐ€์žฅ ํฐ ๋ถ€๋ถ„ ๋ฐฐ์—ด ๊ณฑ์„ ์ €์žฅ
10+
11+
for (let i = 1; i < nums.length; i++) {
12+
let tempMin = Math.min(nums[i], min * nums[i], max * nums[i]);
13+
let tempMax = Math.max(nums[i], min * nums[i], max * nums[i]);
14+
min = tempMin;
15+
max = tempMax;
16+
result = Math.max(result, max);
17+
}
18+
19+
return result;
20+
}
21+
22+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
23+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {string}
5+
*/
6+
const minWindow = function (s, t) {
7+
const counter = Array.from(t).reduce((obj, char) => {
8+
obj[char] = (obj[char] || 0) + 1;
9+
return obj;
10+
}, {});
11+
12+
let lettersToSatisfy = Object.keys(counter).length;
13+
let minStart = 0;
14+
let minEnd = Infinity;
15+
16+
let start = 0;
17+
for (let end = 0; end < s.length; end++) {
18+
// s[end]๊ฐ€ t์— ํฌํ•จ๋œ ๋ฌธ์ž์ธ ๊ฒฝ์šฐ
19+
if (s[end] in counter) {
20+
counter[s[end]]--;
21+
22+
// ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ๋ฒ”์œ„ ์•ˆ์— ๋ชจ๋‘ ํฌํ•จ๋œ ๊ฒฝ์šฐ
23+
if (counter[s[end]] === 0) {
24+
lettersToSatisfy--;
25+
}
26+
}
27+
28+
// ํ˜„์žฌ ๋ฒ”์œ„์— t ๋‚ด ๋ชจ๋“  ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ
29+
while (lettersToSatisfy === 0) {
30+
// ๋” ์ž‘์€ ๋ฒ”์œ„๋กœ ์ •๋‹ต ๊ฐฑ์‹ 
31+
if (end - start < minEnd - minStart) {
32+
minStart = start;
33+
minEnd = end;
34+
}
35+
36+
// start ๋Š˜๋ฆฌ๊ธฐ
37+
if (s[start] in counter) {
38+
counter[s[start]]++;
39+
if (counter[s[start]] > 0) {
40+
lettersToSatisfy++;
41+
}
42+
}
43+
44+
start++;
45+
}
46+
}
47+
48+
return minEnd === Infinity ? '' : s.slice(minStart, minEnd + 1);
49+
};
50+
51+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(m) (m: s.length)
52+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @param {number[][]} heights
3+
* @return {number[][]}
4+
*/
5+
const pacificAtlantic = function (heights) {
6+
// ๊ฐ cell์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋ฐ”๋‹ค๋กœ ๋ป—์–ด๋‚˜๊ฐ€๋Š” ๊ฒƒ ๋ง๊ณ 
7+
// ๋ฐ”๋‹ค์—์„œ ์‹œ์ž‘ํ•ด์„œ ๊ฐ cell์ด ๋ฐ”๋‹ค๋กœ ํ˜๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ ํ›„
8+
// ๋‘ ๋ฐ”๋‹ค์— ๋ชจ๋‘ ํ˜๋Ÿฌ๊ฐˆ ์ˆ˜ ์žˆ๋Š” cell๋งŒ filter
9+
10+
const rows = heights.length;
11+
const cols = heights[0].length;
12+
13+
// ๊ฐ ๋ฐ”๋‹ค์— ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ์ง€ ์—ฌ๋ถ€๋ฅผ ๋‹ด๋Š” visited๋ฅผ ๋งŒ๋“ฆ
14+
const pacific = Array.from({ length: rows }, () => Array(cols).fill(false));
15+
const atlantic = Array.from({ length: rows }, () => Array(cols).fill(false));
16+
17+
const direction = [[0, 1], [0, -1], [1, 0], [-1, 0]];
18+
19+
// ์ˆœํšŒ
20+
function bfs(r, c, visited) {
21+
visited[r][c] = true;
22+
23+
const queue = [[r, c]];
24+
25+
while (queue.length) {
26+
const [r, c] = queue.shift();
27+
28+
for (const [dr, dc] of direction) {
29+
const nr = r + dr;
30+
const nc = c + dc;
31+
32+
// ๋‚˜๋ณด๋‹ค height๊ฐ€ ํฌ๊ณ , ๋ฐฉ๋ฌธํ•œ ์  ์—†์œผ๋ฉด, ํ์— ๋‹ด๊ธฐ
33+
if (0 <= nr && nr < rows && 0 <= nc && nc < cols
34+
&& !visited[nr][nc]
35+
&& heights[nr][nc] >= heights[r][c]
36+
) {
37+
queue.push([nr, nc]);
38+
visited[nr][nc] = true;
39+
}
40+
}
41+
}
42+
}
43+
44+
// ๋ฐ”๋‹ค์—์„œ ์‹œ์ž‘ํ•ด์„œ ๊ฑฐ๊พธ๋กœ ํƒ์ƒ‰
45+
for (let r = 0; r < rows; r++) {
46+
bfs(r, 0, pacific); // left
47+
bfs(r, cols - 1, atlantic); // right
48+
}
49+
50+
for (let c = 0; c < cols; c++) {
51+
bfs(0, c, pacific); // top
52+
bfs(rows - 1, c, atlantic); // bottom
53+
}
54+
55+
// ํƒœํ‰์–‘, ๋Œ€์„œ์–‘์œผ๋กœ ๋ชจ๋‘ flowํ•  ์ˆ˜ ์žˆ๋Š” cell ์ฐพ๊ธฐ
56+
const result = [];
57+
58+
for (let r = 0; r < rows; r++) {
59+
for (let c = 0; c < cols; c++) {
60+
if (pacific[r][c] && atlantic[r][c]) {
61+
result.push([r, c]);
62+
}
63+
}
64+
}
65+
66+
return result;
67+
};
68+
69+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(m*n)
70+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(m*n)

โ€Žsum-of-two-integers/uraflower.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* +, - ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  a, b๋ฅผ ๋”ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {number} a
4+
* @param {number} b
5+
* @return {number}
6+
*/
7+
const getSum = function (a, b) {
8+
while (b !== 0) {
9+
[a, b] = [a ^ b, (a & b) << 1]
10+
}
11+
12+
return a;
13+
};
14+
15+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(1)
16+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

0 commit comments

Comments
ย (0)