Skip to content

Commit 3f22d42

Browse files
authored
Merge pull request #1525 from soobing/week9
[soobing] WEEK09 Solutions
2 parents e370998 + 61a6b72 commit 3f22d42

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed

โ€Žlinked-list-cycle/soobing.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* ๋ฌธ์ œ ์„ค๋ช…
3+
* - ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ ๋‚ด์— cycle์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ
4+
*
5+
* ์•„์ด๋””์–ด
6+
* - ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ๋ฅผ ๋๊นŒ์ง€ ์ˆœํšŒํ•˜๋ฉด์„œ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•˜๊ณ , ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•˜๋ฉด cycle์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํŒ๋‹จ
7+
* - ์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„๋ณต์žก๋„ O(n)
8+
*
9+
*/
10+
11+
class ListNode {
12+
val: number;
13+
next: ListNode | null;
14+
constructor(val?: number, next?: ListNode | null) {
15+
this.val = val === undefined ? 0 : val;
16+
this.next = next === undefined ? null : next;
17+
}
18+
}
19+
20+
function hasCycle(head: ListNode | null): boolean {
21+
const visited = new Set<ListNode>();
22+
23+
while (head) {
24+
if (visited.has(head)) {
25+
return true;
26+
} else {
27+
visited.add(head);
28+
head = head.next;
29+
}
30+
}
31+
return false;
32+
}
33+
34+
/**
35+
* ๊ณต๊ฐ„๋ณต์žก๋„ O(1) ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ์Œ.
36+
*/
37+
38+
// function hasCycle(head: ListNode | null): boolean {
39+
// let slow = head;
40+
// let fast = head;
41+
42+
// while (fast && fast.next) {
43+
// slow = slow!.next;
44+
// fast = fast.next.next!;
45+
46+
// if (slow === fast) {
47+
// return true;
48+
// }
49+
// }
50+
51+
// return false;
52+
// }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* ๋ฌธ์ œ ์„ค๋ช…
3+
* - ๋ฐฐ์—ด์—์„œ ์—ฐ์†๋œ ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ๊ณฑ์ด ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ฐพ๋Š” ๋ฌธ์ œ
4+
*
5+
* ์•„์ด๋””์–ด
6+
* - 1) ๋ธŒ๋ฃจํŠธํฌ์Šค O(n^2)
7+
* - 2) DP ์ตœ์ ํ™” O(n)
8+
* - ๋งค index๋งˆ๋‹ค, ํ˜„์žฌ๊นŒ์ง€ max ๊ณฑ, min ๊ณฑ์„ ์ฐพ๊ณ  ์ตœ๋Œ€๊ฐ’์„ ๊ฐฑ์‹ 
9+
*/
10+
function maxProduct(nums: number[]): number {
11+
let maxSoFar = nums[0];
12+
let max = nums[0];
13+
let min = nums[0];
14+
15+
for (let i = 1; i < nums.length; i++) {
16+
const candidate = [nums[i], max * nums[i], min * nums[i]];
17+
max = Math.max(...candidate);
18+
min = Math.min(...candidate);
19+
maxSoFar = Math.max(maxSoFar, max);
20+
}
21+
return maxSoFar;
22+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* ๋ฌธ์ œ ์„ค๋ช…
3+
* - ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s์—์„œ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด t์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ์ตœ์†Œ ์œˆ๋„์šฐ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ
4+
*
5+
* ์•„์ด๋””์–ด
6+
* - ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ + ํ•ด์‹œ๋งต(need, window)์„ ์ด์šฉํ•˜์—ฌ ํ’€์ดํ•œ๋‹ค.
7+
* - ์˜ค๋ฅธ์ชฝ ํฌ์ธํ„ฐ๋ฅผ ๋จผ์ € ์ด๋™ํ•˜๊ณ , ์ด๋ฏธ ๋ชจ๋‘ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์™ผ์ชฝ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•˜์—ฌ ์ตœ์†Œ ์œˆ๋„์šฐ๋ฅผ ์ฐพ๋Š”๋‹ค.
8+
*
9+
*/
10+
function minWindow(s: string, t: string): string {
11+
if (t.length > s.length) return "";
12+
13+
const need = new Map<string, number>();
14+
const window = new Map<string, number>();
15+
16+
for (const char of t) {
17+
need.set(char, (need.get(char) || 0) + 1);
18+
}
19+
20+
let have = 0;
21+
let needSize = need.size;
22+
let res = [-1, -1];
23+
let resLen = Infinity;
24+
25+
let left = 0;
26+
27+
for (let right = 0; right < s.length; right++) {
28+
const c = s[right];
29+
window.set(c, (window.get(c) || 0) + 1);
30+
31+
if (need.has(c) && window.get(c) === need.get(c)) {
32+
have++;
33+
}
34+
35+
while (have === needSize) {
36+
const currentResLen = right - left + 1;
37+
if (currentResLen < resLen) {
38+
res = [left, right];
39+
resLen = currentResLen;
40+
}
41+
42+
const lChar = s[left];
43+
window.set(lChar, window.get(lChar)! - 1);
44+
45+
if (need.has(lChar) && window.get(lChar)! < need.get(lChar)!) {
46+
have--;
47+
}
48+
left++;
49+
}
50+
}
51+
52+
const [start, end] = res;
53+
return resLen === Infinity ? "" : s.slice(start, end + 1);
54+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* ๋ฌธ์ œ ์„ค๋ช…
3+
* - ๋น„๊ฐ€ ๋‚ด๋ ธ์„ ๋•Œ, ํƒœํ‰์–‘๊ณผ ๋Œ€์„œ์–‘ ๋ชจ๋‘๋กœ ๋ฌผ์ด ํ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์ง€์ ์„ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
4+
* ์•„์ด๋””์–ด
5+
* 1) BFS/DFS
6+
* - ๊ฐ ๋ฐ”๋‹ค์—์„œ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ๋ฌผ์ด ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์…€์„ ํƒ์ƒ‰ํ•œ ํ›„, ๋‘ ๋ฐ”๋‹ค ๋ชจ๋‘์— ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ์…€์˜ ๊ต์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
7+
*
8+
*/
9+
10+
function pacificAtlantic(heights: number[][]): number[][] {
11+
const m = heights.length;
12+
const n = heights[0].length;
13+
14+
const pacific = Array.from({ length: m }, () => Array(n).fill(false));
15+
const atlantic = Array.from({ length: m }, () => Array(n).fill(false));
16+
17+
const directions = [
18+
[1, 0],
19+
[-1, 0],
20+
[0, 1],
21+
[0, -1],
22+
];
23+
24+
function dfs(r: number, c: number, visited: boolean[][], prevHeight: number) {
25+
if (
26+
r < 0 ||
27+
c < 0 ||
28+
r >= m ||
29+
c >= n ||
30+
visited[r][c] ||
31+
heights[r][c] < prevHeight
32+
)
33+
return;
34+
35+
visited[r][c] = true;
36+
37+
for (const [dr, dc] of directions) {
38+
dfs(r + dr, c + dc, visited, heights[r][c]);
39+
}
40+
}
41+
42+
// ํƒœํ‰์–‘ DFS
43+
for (let i = 0; i < m; i++) {
44+
dfs(i, 0, pacific, heights[i][0]); // ์™ผ์ชฝ
45+
dfs(i, n - 1, atlantic, heights[i][n - 1]); // ์˜ค๋ฅธ์ชฝ
46+
}
47+
48+
for (let j = 0; j < n; j++) {
49+
dfs(0, j, pacific, heights[0][j]); // ์œ„์ชฝ
50+
dfs(m - 1, j, atlantic, heights[m - 1][j]); // ์•„๋ž˜์ชฝ
51+
}
52+
53+
const result: number[][] = [];
54+
55+
for (let i = 0; i < m; i++) {
56+
for (let j = 0; j < n; j++) {
57+
if (pacific[i][j] && atlantic[i][j]) {
58+
result.push([i, j]);
59+
}
60+
}
61+
}
62+
63+
return result;
64+
}

โ€Žsum-of-two-integers/soobing.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* ๋ฌธ์ œ ์„ค๋ช…
3+
* - ๋‘ ์ •์ˆ˜์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ (๋น„ํŠธ ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ)
4+
*
5+
* ์•„์ด๋””์–ด
6+
* - ๋ฐ˜์˜ฌ๋ฆผ์€ AND(&) + ์™ผ์ชฝ shift 1, ๋ง์…ˆ์€ XOR(^) ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ ํ•œ๋‹ค.
7+
* - ๋ฐ˜์˜ฌ๋ฆผ์ด 0์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. โญ๏ธ
8+
*/
9+
function getSum(a: number, b: number): number {
10+
while (b !== 0) {
11+
const carry = (a & b) << 1;
12+
a = a ^ b;
13+
b = carry;
14+
}
15+
return a;
16+
}

0 commit comments

Comments
ย (0)