Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4148561

Browse files
authoredFeb 9, 2025
Merge pull request #999 from mike2ox/main
[moonhyeok] Week9
2 parents f0389b2 + d9f2806 commit 4148561

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
 
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Source: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
3+
* ํ’€์ด๋ฐฉ๋ฒ•: ์ด์ง„ ํƒ์ƒ‰์„ ์ด์šฉํ•˜์—ฌ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์Œ
4+
*
5+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(log n)
6+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
7+
*/
8+
9+
function findMin(nums: number[]): number {
10+
// ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 1์ธ ๊ฒฝ์šฐ
11+
if (nums.length === 1) return nums[0];
12+
13+
let left: number = 0;
14+
let right: number = nums.length - 1;
15+
16+
// ์ด๋ฏธ ์ •๋ ฌ๋œ ๊ฒฝ์šฐ
17+
if (nums[right] > nums[0]) {
18+
return nums[0];
19+
}
20+
21+
// ์ด์ง„ ํƒ์ƒ‰
22+
while (left <= right) {
23+
const mid: number = Math.floor((left + right) / 2);
24+
25+
// ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ๋Š” ์กฐ๊ฑด๋“ค
26+
if (nums[mid] > nums[mid + 1]) {
27+
return nums[mid + 1];
28+
}
29+
if (nums[mid - 1] > nums[mid]) {
30+
return nums[mid];
31+
}
32+
33+
// ํƒ์ƒ‰ ๋ฒ”์œ„ ์กฐ์ •
34+
if (nums[mid] > nums[0]) {
35+
// ์ตœ์†Ÿ๊ฐ’์€ ์ค‘๊ฐ„์  ์ดํ›„์— ์žˆ์Œ
36+
left = mid + 1;
37+
} else {
38+
// ์ตœ์†Ÿ๊ฐ’์€ ์ค‘๊ฐ„์  ์ด์ „์— ์žˆ์Œ
39+
right = mid - 1;
40+
}
41+
}
42+
43+
return nums[0]; // ๊ธฐ๋ณธ๊ฐ’ ๋ฐ˜ํ™˜
44+
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Source: https://leetcode.com/problems/linked-list-cycle/
3+
* ํ’€์ด๋ฐฉ๋ฒ•: Set์„ ์ด์šฉํ•˜์—ฌ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ˆœํšŒํ•˜๋ฉด์„œ ์ค‘๋ณต๋œ ๋…ธ๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
4+
*
5+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
6+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
7+
*/
8+
9+
// class ListNode {
10+
// val: number;
11+
// next: ListNode | null;
12+
// constructor(val?: number, next?: ListNode | null) {
13+
// this.val = val === undefined ? 0 : val;
14+
// this.next = next === undefined ? null : next;
15+
// }
16+
// }
17+
18+
function hasCycle(head: ListNode | null): boolean {
19+
if (head === null) return false;
20+
21+
// ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋“ค์„ ์ €์žฅํ•  Set
22+
const addr = new Set<ListNode>();
23+
24+
// ์ฒซ ๋…ธ๋“œ ์ถ”๊ฐ€
25+
addr.add(head);
26+
head = head.next;
27+
28+
// ๋ฆฌ์ŠคํŠธ ์ˆœํšŒ
29+
while (head !== null) {
30+
// ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์ธ ๊ฒฝ์šฐ cycle ์กด์žฌ
31+
if (addr.has(head)) return true;
32+
33+
// ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ถ”๊ฐ€
34+
addr.add(head);
35+
head = head.next;
36+
}
37+
38+
return false;
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Source: https://leetcode.com/problems/maximum-product-subarray/
3+
* ํ’€์ด๋ฐฉ๋ฒ•: ํ˜„์žฌ ๊ณฑ๊ณผ ์ตœ๋Œ€ ๊ณฑ์„ ๋น„๊ตํ•˜์—ฌ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•จ
4+
*
5+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
6+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
7+
*
8+
* ๋‹ค๋ฅธ ํ’€์ด๋ฐฉ๋ฒ•
9+
* - DP๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€์ด
10+
*/
11+
function maxProduct(nums: number[]): number {
12+
if (nums.length === 0) return 0;
13+
14+
let maxProduct = nums[0];
15+
let currentProduct = 1;
16+
17+
// ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ˆœํšŒ
18+
for (let i = 0; i < nums.length; i++) {
19+
currentProduct *= nums[i];
20+
maxProduct = Math.max(maxProduct, currentProduct);
21+
// ํ˜„์žฌ ๊ณฑ์ด 0์ด ๋˜๋ฉด ๋ฆฌ์…‹
22+
if (currentProduct === 0) {
23+
currentProduct = 1;
24+
}
25+
}
26+
27+
currentProduct = 1;
28+
29+
// ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ˆœํšŒ
30+
for (let i = nums.length - 1; i >= 0; i--) {
31+
currentProduct *= nums[i];
32+
maxProduct = Math.max(maxProduct, currentProduct);
33+
// ํ˜„์žฌ ๊ณฑ์ด 0์ด ๋˜๋ฉด ๋ฆฌ์…‹
34+
if (currentProduct === 0) {
35+
currentProduct = 1;
36+
}
37+
}
38+
39+
return maxProduct;
40+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Source: https://leetcode.com/problems/pacific-atlantic-water-flow/
3+
* ํ’€์ด๋ฐฉ๋ฒ•: DFS๋ฅผ ์ด์šฉํ•˜์—ฌ pacific๊ณผ atlantic์—์„œ ๋ฌผ์ด ํ๋ฅด๋Š” ์ง€์ ์„ ์ฐพ์Œ
4+
*
5+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n * m) (n: ํ–‰์˜ ๊ฐœ์ˆ˜, m: ์—ด์˜ ๊ฐœ์ˆ˜)
6+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(n * m)
7+
*/
8+
9+
function pacificAtlantic(heights: number[][]): number[][] {
10+
if (!heights || !heights[0]) return [];
11+
12+
const rows = heights.length;
13+
const cols = heights[0].length;
14+
15+
// checklist
16+
const pacific = new Set<string>();
17+
const atlantic = new Set<string>();
18+
19+
// DFS
20+
function dfs(
21+
row: number,
22+
col: number,
23+
prevHeight: number,
24+
visited: Set<string>
25+
) {
26+
// row, col์ด ๊ฒฝ๊ณ„ ๋ฐ–์ด๊ฑฐ๋‚˜ ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๊ฑฐ๋‚˜ ์ด์ „ ๋†’์ด๋ณด๋‹ค ๋‚ฎ์€ ๊ฒฝ์šฐ
27+
if (
28+
row < 0 ||
29+
row >= rows ||
30+
col < 0 ||
31+
col >= cols ||
32+
heights[row][col] < prevHeight ||
33+
visited.has(`${row},${col}`)
34+
) {
35+
return;
36+
}
37+
38+
// ํ˜„์žฌ ์œ„์น˜ ์ฒดํฌ
39+
visited.add(`${row},${col}`);
40+
41+
// 4๋ฐฉํ–ฅ ํƒ์ƒ‰
42+
dfs(row + 1, col, heights[row][col], visited);
43+
dfs(row - 1, col, heights[row][col], visited);
44+
dfs(row, col + 1, heights[row][col], visited);
45+
dfs(row, col - 1, heights[row][col], visited);
46+
}
47+
48+
// 0,0์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ ์‹œ์ž‘
49+
for (let col = 0; col < cols; col++) {
50+
dfs(0, col, heights[0][col], pacific);
51+
}
52+
for (let row = 0; row < rows; row++) {
53+
dfs(row, 0, heights[row][0], pacific);
54+
}
55+
56+
// rows-1, cols-1(pacificํ•˜๊ณ ๋Š” ์ •๋ฐ˜๋Œ€ ์œ„์น˜์—์„œ ์‹œ์ž‘)
57+
for (let col = 0; col < cols; col++) {
58+
dfs(rows - 1, col, heights[rows - 1][col], atlantic);
59+
}
60+
for (let row = 0; row < rows; row++) {
61+
dfs(row, cols - 1, heights[row][cols - 1], atlantic);
62+
}
63+
64+
const result: number[][] = [];
65+
for (let row = 0; row < rows; row++) {
66+
for (let col = 0; col < cols; col++) {
67+
const pos = `${row},${col}`;
68+
if (pacific.has(pos) && atlantic.has(pos)) {
69+
result.push([row, col]);
70+
}
71+
}
72+
}
73+
74+
return result;
75+
}

0 commit comments

Comments
 (0)
Please sign in to comment.