Skip to content

Commit 113af65

Browse files
authored
Merge pull request #985 from mmyeon/main
[mallayon] Week 9
2 parents 8d7381b + 0d0395b commit 113af65

File tree

5 files changed

+265
-0
lines changed

5 files changed

+265
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @link https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
5+
* - O(logn)์œผ๋กœ ํ’€์–ด์•ผ ํ•˜๋‹ˆ๊นŒ ์ด์ง„ ํƒ์ƒ‰ ์ ์šฉ
6+
* - ๋ฐฐ์—ด์˜ start, end ์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•ด์„œ ์ตœ์†Œ๊ฐ’์ด ์žˆ๋Š” ๋ฐฉํ–ฅ์„ ํƒ์ƒ‰
7+
* - nums[mid] > nums[end]์ด๋ฉด, ์ตœ์†Œ๊ฐ’์ด ์˜ค๋ฅธ์ชฝ์— ์žˆ์œผ๋‹ˆ๊นŒ start๋ฅผ mid+1๋กœ ์ด๋™
8+
* - ๋ฐ˜๋Œ€๋กœ nums[mid] < nums[end] ์ด๋ฉด, ์ตœ์†Œ๊ฐ’์ด ์™ผ์ชฝ์— ์žˆ์œผ๋‹ˆ๊นŒ end๋ฅผ mid๋กœ ์ด๋™
9+
*
10+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(logn)
11+
* - ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ๊ณ„์† ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์ด๋‹ˆ๊นŒ O(logn)
12+
*
13+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(1)
14+
* - ๊ณ ์ •๋œ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉ
15+
*/
16+
function findMin(nums: number[]): number {
17+
let start = 0,
18+
end = nums.length - 1;
19+
20+
while (start < end) {
21+
let mid = Math.floor(start + (end - start) / 2);
22+
23+
if (nums[mid] > nums[end]) start = mid + 1;
24+
else end = mid;
25+
}
26+
27+
return nums[start];
28+
}

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = val === undefined ? 0 : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}
9+
10+
/**
11+
* @link https://leetcode.com/problems/linked-list-cycle/description/
12+
*
13+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
14+
* - ๋…ธ๋“œ ์ˆœํšŒํ•˜๋ฉด์„œ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์— ์ €์žฅ
15+
* - ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ๊ฐ€ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์— ์žˆ์œผ๋ฉด ์ˆœํ™˜ ๊ตฌ์กฐ ์ด๋ฏ€๋กœ true ๋ฆฌํ„ด
16+
*
17+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
18+
* - ์ˆœํ™˜ ์ด์ „ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ n๋งŒํผ ์ˆœํšŒํ•˜๋ฉด์„œ ์ˆœํ™˜ ์—ฌ๋ถ€ ํ™•์ธ
19+
*
20+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
21+
* - visited set์— ์ˆœํ™˜๋˜๊ธฐ ์ด์ „ ๋…ธ๋“œ n๊ฐœ ์ €์žฅ
22+
*/
23+
function hasCycle(head: ListNode | null): boolean {
24+
const visited = new Set<ListNode>();
25+
let current = head;
26+
27+
while (current !== null) {
28+
if (visited.has(current)) return true;
29+
30+
visited.add(current);
31+
current = current.next;
32+
}
33+
34+
return false;
35+
}
36+
37+
/*
38+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
39+
* - ๊ณต๊ฐ„๋ณต์žก๋„ O(1)๋กœ ํ’€์ด
40+
* - ์‚ฌ์ดํด์ด ์žˆ์œผ๋ฉด ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๊ฒฐ๊ตญ ๊ฐ™์€ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋˜๋ฏ€๋กœ ํˆฌ ํฌ์ธํ„ฐ ์‚ฌ์šฉ
41+
* - ์‚ฌ์ดํด์ด ์—†๋Š” ๊ฒฝ์šฐ๋ฅผ ์œ„ํ•ด์„œ tail๋…ธ๋“œ์˜ null์ฒดํฌ๋ฅผ ํ•ด์•ผํ•จ
42+
*
43+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
44+
* - ์ˆœํ™˜ ์ด์ „ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ n๋งŒํผ ์ˆœํšŒํ•˜๋ฉด์„œ ์ˆœํ™˜ ์—ฌ๋ถ€ ํ™•์ธ
45+
*
46+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(1)
47+
* - ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์—†์ด slow, fast ํฌ์ธํ„ฐ๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(1)
48+
*/
49+
function hasCycle(head: ListNode | null): boolean {
50+
let slow = head;
51+
let fast = head;
52+
53+
while (fast !== null && fast.next !== null) {
54+
slow = slow.next;
55+
fast = fast.next.next;
56+
57+
if (slow === fast) return true;
58+
}
59+
60+
return false;
61+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @link https://leetcode.com/problems/maximum-product-subarray/
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
5+
* - ์Œ์ˆ˜๊ฐ€ ์ง์ˆ˜๋ฒˆ ๋‚˜์˜ค๋ฉด ์ตœ๋Œ€๊ฐ’์ด ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ ์ตœ์†Œ๊ณฑ, ์ตœ๋Œ€๊ณฑ์„ ๋ชจ๋‘ ์—…๋ฐ์ดํŠธ
6+
* - ํ˜„์žฌ ๊ฐ’์ด ๋‹จ๋…์œผ๋กœ ์ตœ์†Œ, ์ตœ๋Œ€์ผ ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ ํ˜„์žฌ๊ฐ’, ์ตœ์†Œ๊ณฑ * ํ˜„์žฌ๊ฐ’, ์ตœ๋Œ€๊ณฑ * ํ˜„์žฌ๊ฐ’์„ ๋น„๊ต
7+
*
8+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
9+
* - nums 1ํšŒ ์ˆœํšŒ
10+
*
11+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(1)
12+
* - ๊ณ ์ •๋œ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉ
13+
*/
14+
function maxProduct(nums: number[]): number {
15+
let currentMin = nums[0],
16+
currentMax = nums[0],
17+
maxSoFar = nums[0];
18+
19+
for (let i = 1; i < nums.length; i++) {
20+
const num = nums[i];
21+
const minCandidate = currentMin * num;
22+
const maxCandidate = currentMax * num;
23+
24+
currentMin = Math.min(num, minCandidate, maxCandidate);
25+
currentMax = Math.max(num, minCandidate, maxCandidate);
26+
27+
maxSoFar = Math.max(currentMax, maxSoFar);
28+
}
29+
30+
return maxSoFar;
31+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @link https://leetcode.com/problems/minimum-window-substring/description/
3+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• : 2๊ฐœ์˜ ํฌ์ธํ„ฐ ํ™œ์šฉํ•ด์„œ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๋ฐฉ์‹ ์‚ฌ์šฉ
4+
* - t์˜ ๋ฌธ์ž๋ฅผ ๋งต์— ์ €์žฅํ•ด์„œ ๊ฐœ์ˆ˜ ๊ธฐ๋ก
5+
* - right ํฌ์ธํ„ฐ๋กœ t์˜ ๋ชจ๋“  ๋ฌธ์ž ํฌํ•จํ•  ๋•Œ๊นŒ์ง€ ์œˆ๋„์šฐ ํ™•์žฅ
6+
* - ๋ชจ๋“  ๋ฌธ์ž ํฌํ•จํ•˜๋ฉด, left ํฌ์ธํ„ฐ๋กœ ์ตœ์†Œ ์œˆ๋„์šฐ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ์œˆ๋„์šฐ ์ถ•์†Œ
7+
* - 'ํ™•์žฅ => ์ถ•์†Œ => ์ตœ์†Œ ์œˆ๋„์šฐ ์—…๋ฐ์ดํŠธ' ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณต
8+
*
9+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
10+
* - n์€ s์˜ ๊ธธ์ด, ๊ฐ ๋ฌธ์ž ์ตœ๋Œ€ 2ํšŒ ๋ฐฉ๋ฌธ (ํ™•์žฅ + ์ถ•์†Œ)
11+
*
12+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
13+
* - ์ตœ์•…์˜ ๊ฒฝ์šฐ, ์œˆ๋„์šฐ์— s์˜ ๋ชจ๋“  ๋ฌธ์ž๊ฐ€ ์ €์žฅ๋จ
14+
*/
15+
function minWindow(s: string, t: string): string {
16+
const targetCharCount = new Map<string, number>();
17+
18+
// t์˜ ๋ฌธ์ž ๊ฐœ์ˆ˜ ์นด์šดํŠธ
19+
for (const char of t) {
20+
targetCharCount.set(char, (targetCharCount.get(char) ?? 0) + 1);
21+
}
22+
23+
const requiredUniqueChars = targetCharCount.size;
24+
let matchedUniqueChars = 0;
25+
const windowCharCount = new Map<string, number>();
26+
27+
let minWindow = "";
28+
let minWindowLength = Infinity;
29+
30+
let left = 0,
31+
right = 0;
32+
33+
while (right < s.length) {
34+
const char = s[right];
35+
windowCharCount.set(char, (windowCharCount.get(char) ?? 0) + 1);
36+
37+
// t์— ์†ํ•˜๋Š” ๋ฌธ์ž์ด๋ฉด์„œ, ๋ฌธ์ž ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ
38+
if (
39+
targetCharCount.has(char) &&
40+
targetCharCount.get(char) === windowCharCount.get(char)
41+
)
42+
matchedUniqueChars++;
43+
44+
while (matchedUniqueChars === requiredUniqueChars) {
45+
const windowLength = right - left + 1;
46+
47+
// ์ตœ์†Œ ์œˆ๋„์šฐ ์—…๋ฐ์ดํŠธ
48+
if (windowLength < minWindowLength) {
49+
minWindowLength = windowLength;
50+
minWindow = s.substring(left, right + 1);
51+
}
52+
53+
const leftChar = s[left];
54+
windowCharCount.set(leftChar, windowCharCount.get(leftChar)! - 1);
55+
56+
//์ถ•์†Œ๋กœ ์œˆ๋„์šฐ ๋‚ด์˜ t๋ฌธ์ž๊ฐ€ ๊ฐ์†Œํ–ˆ์œผ๋ฉด matchedUniqueChars ๊ฐ์†Œ
57+
if (windowCharCount.get(leftChar)! < targetCharCount.get(leftChar)!)
58+
matchedUniqueChars--;
59+
60+
// ์œˆ๋„์šฐ ์ถ•์†Œ
61+
left++;
62+
}
63+
64+
// ์œˆ๋„์šฐ ํ™•์žฅ
65+
right++;
66+
}
67+
68+
return minWindow;
69+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @link https://leetcode.com/problems/pacific-atlantic-water-flow/description/
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
5+
* - pacific, atlantic๋ฅผ ๋™์‹œ์— ๋„๋‹ฌํ•˜๋Š” ์ง€์  ์ฐพ๊ธฐ ์œ„ํ•ด์„œ, ๋„๋‹ฌ ์—ฌ๋ถ€ ํŠธ๋ž˜ํ‚นํ•˜๋Š” 2๊ฐœ์˜ visited ๋ฐฐ์—ด ์‚ฌ์šฉํ•œ๋‹ค.
6+
* - ๋ฐ”๋‹ค ๊ฒฝ๊ณ„์—์„œ๋งŒ DFS๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ์ฒดํฌํ•œ๋‹ค.
7+
* - ๋ฐ”๋‹ค ๊ฒฝ๊ณ„์—์„œ ์‹œ์ž‘ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— DFS๋Š” ์ธ์ ‘ ์…€์˜ ๋†’์ด๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ ๋†’์„ ๋•Œ๋งŒ ํ˜ธ์ถœ๋˜์–ด์•ผ ํ•œ๋‹ค.
8+
*
9+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(m * n)
10+
* - ๊ฐ ์…€๋งˆ๋‹ค ์ตœ๋Œ€ 4๋ฒˆ DFS๊ฐ€ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๋‹ค. O(m * n)
11+
* - ๊ฒฐ๊ณผ ์ˆœํšŒํ•  ๋•Œ m * n ๋งŒํผ ์ˆœํšŒํ•œ๋‹ค.
12+
*
13+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(m * n)
14+
* - 2๊ฐœ์˜ visited ๋ฐฐ์—ด ์‚ฌ์šฉํ•œ๋‹ค.
15+
* - ์ตœ์•…์˜ ๊ฒฝ์šฐ, m * n ๋ชจ๋“  ์นธ์—์„œ DFS ํ˜ธ์ถœ๋œ๋‹ค.
16+
*/
17+
18+
const directions = [
19+
[-1, 0],
20+
[1, 0],
21+
[0, -1],
22+
[0, 1],
23+
];
24+
25+
function pacificAtlantic(heights: number[][]): number[][] {
26+
const result: number[][] = [];
27+
28+
const rows = heights.length;
29+
const cols = heights[0].length;
30+
31+
// ๋‘ ๋ฐ”๋‹ค ๋„๋‹ฌํ•˜๋Š” ์ง€์  ํŠธ๋ž˜ํ‚น ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฐ์—ด
32+
const pacificVisited = Array.from({ length: rows }, () =>
33+
Array(cols).fill(false)
34+
);
35+
const atlanticVisited = Array.from({ length: rows }, () =>
36+
Array(cols).fill(false)
37+
);
38+
39+
const dfs = (row: number, col: number, visited: boolean[][]) => {
40+
if (visited[row][col]) return;
41+
// ๋ฐฉ๋ฌธ ์ง€์  ๊ธฐ๋ก
42+
visited[row][col] = true;
43+
44+
for (const [x, y] of directions) {
45+
const newRow = row + x;
46+
const newCol = col + y;
47+
48+
// ์ƒˆ๋กœ์šด ์œ„์น˜๊ฐ€ ๊ฒฝ๊ณ„์•ˆ์— ์žˆ๊ณ , ํ˜„์žฌ ๋†’์ด๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ๋†’์„ ๋•Œ๋งŒ DFS ํ˜ธ์ถœ
49+
if (
50+
0 <= newRow &&
51+
newRow < rows &&
52+
0 <= newCol &&
53+
newCol < cols &&
54+
heights[newRow][newCol] >= heights[row][col]
55+
)
56+
dfs(newRow, newCol, visited);
57+
}
58+
};
59+
60+
// pacific ๊ฒฝ๊ณ„์—์„œ DFS ํ˜ธ์ถœ (์ฒซ ๋ฒˆ์จฐ ์—ด, ์ฒซ ๋ฒˆ์งธ ํ–‰)
61+
for (let row = 0; row < rows; row++) dfs(row, 0, pacificVisited);
62+
for (let col = 0; col < cols; col++) dfs(0, col, pacificVisited);
63+
64+
// atlantic ๊ฒฝ๊ณ„์—์„œ DFS ํ˜ธ์ถœ (๋งˆ์ง€๋ง‰ ์—ด, ๋งˆ์ง€๋ง‰ ํ–‰)
65+
for (let row = 0; row < rows; row++) dfs(row, cols - 1, atlanticVisited);
66+
for (let col = 0; col < cols; col++) dfs(rows - 1, col, atlanticVisited);
67+
68+
for (let row = 0; row < rows; row++) {
69+
for (let col = 0; col < cols; col++) {
70+
if (pacificVisited[row][col] && atlanticVisited[row][col])
71+
result.push([row, col]);
72+
}
73+
}
74+
75+
return result;
76+
}

0 commit comments

Comments
ย (0)