Skip to content

Commit 6d1380b

Browse files
authoredJan 24, 2025
Merge pull request #941 from mmyeon/main
[mallayon] Week 7
ยท
v4.15.0v3.7.0
2 parents 89edeec + 0527960 commit 6d1380b

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed
 
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
*@link https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
5+
* - ์Šฌ๋ผ์šฐ๋”ฉ ์œˆ๋„์šฐ์™€ set ์‚ฌ์šฉํ•ด์„œ ์ค‘๋ณต์—†๋Š” ๋ฌธ์ž์—ด ํ™•์ธ
6+
* - ์ค‘๋ณต ๋ฌธ์ž ๋ฐœ๊ฒฌํ•˜๋ฉด ์œˆ๋„์šฐ ์ถ•์†Œ
7+
*
8+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
9+
* - ๊ฐ ๋ฌธ์ž ์ˆœํšŒํ•˜๋‹ˆ๊นŒ
10+
*
11+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
12+
* - ์ค‘๋ณต ์—†๋Š” ๊ฒฝ์šฐ ์ตœ๋Œ€ n๊ฐœ์˜ ๋ฌธ์ž set์— ์ €์žฅ
13+
*/
14+
function lengthOfLongestSubstring(s: string): number {
15+
let start = 0,
16+
end = 0,
17+
maxLength = 0;
18+
const set = new Set<string>();
19+
20+
while (end < s.length) {
21+
const char = s[end];
22+
23+
// ์ค‘๋ณต์ด ์žˆ์œผ๋ฉด ์œˆ๋„์šฐ ์ถ•์†Œ
24+
if (set.has(char)) {
25+
set.delete(s[start]);
26+
start++;
27+
} else {
28+
// ์ค‘๋ณต ์—†์œผ๋ฉด set์— ๋ฌธ์ž ์ถ”๊ฐ€, ์œˆ๋„์šฐ ํ™•์žฅ
29+
set.add(char);
30+
maxLength = Math.max(maxLength, end - start + 1);
31+
end++;
32+
}
33+
}
34+
35+
return maxLength;
36+
}
37+
38+
// Map ์‚ฌ์šฉํ•ด์„œ ์ค‘๋ณต ๋ฐœ์ƒ์‹œ start ์ธ๋ฑ์Šค๊ฐ€ ์ ํ”„ํ•˜๋„๋ก ๊ฐœ์„ 
39+
function lengthOfLongestSubstring(s: string): number {
40+
let start = 0,
41+
maxLength = 0;
42+
const map = new Map<string, number>();
43+
44+
for (let i = 0; i < s.length; i++) {
45+
const char = s[i];
46+
// ์ค‘๋ณต ์žˆ๋Š” ๊ฒฝ์šฐ, ์ค‘๋ณต๋ฌธ์ž์˜ ๋‹ค์Œ ์œ„์น˜๋กœ ์ ํ”„
47+
if (map.has(char)) start = Math.max(start, map.get(char)! + 1);
48+
// ์ธ๋ฑ์Šค ๊ฐฑ์‹ 
49+
map.set(char, i);
50+
51+
maxLength = Math.max(maxLength, i - start + 1);
52+
}
53+
54+
return maxLength;
55+
}

โ€Žnumber-of-islands/mmyeon.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
*@link https://leetcode.com/problems/number-of-islands/
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
5+
* - ์„ฌ์˜ ์‹œ์ž‘์ ์—์„œ ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋ฏ€๋กœ DFS ์‚ฌ์šฉ
6+
* - ๋ฐฉ๋ฌธํ•œ ์„ฌ์€ ์ค‘๋ณต ํƒ์ƒ‰ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐฉ๋ฌธ ํ›„ ๊ฐ’ ๋ณ€๊ฒฝ ์ฒ˜๋ฆฌ
7+
*
8+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(m * n)
9+
* - 2์ฐจ์› ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(m * n)
10+
*
11+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(m * n)
12+
* - DFS ํ˜ธ์ถœ ์Šคํƒ ์ตœ๋Œ€ ๊นŠ์ด๊ฐ€ m * n ๋งŒํผ ์Œ“์ผ ์ˆ˜ ์žˆ๋‹ค.
13+
*/
14+
15+
function numIslands(grid: string[][]): number {
16+
let count = 0;
17+
const rows = grid.length;
18+
const cols = grid[0].length;
19+
20+
// ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰ ๋ฐฉํ–ฅ ๋ฐฐ์—ด
21+
const directions = [
22+
[0, -1],
23+
[0, 1],
24+
[-1, 0],
25+
[1, 0],
26+
];
27+
28+
// ํ˜„์žฌ ์œ„์น˜์—์„œ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์„ฌ ํƒ์ƒ‰
29+
const dfs = (row: number, col: number) => {
30+
// ์ข…๋ฃŒ ์กฐ๊ฑด : ๋ฒ”์œ„ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜, ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ฒฝ์šฐ
31+
if (
32+
row < 0 ||
33+
row >= rows ||
34+
col < 0 ||
35+
col >= cols ||
36+
grid[row][col] === "0"
37+
)
38+
return;
39+
40+
// ํ˜„์žฌ ์œ„์น˜ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
41+
grid[row][col] = "0";
42+
43+
// ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰
44+
for (const [x, y] of directions) {
45+
dfs(row + x, col + y);
46+
}
47+
};
48+
49+
for (let row = 0; row < rows; row++) {
50+
for (let col = 0; col < cols; col++) {
51+
// ์„ฌ์˜ ์‹œ์ž‘์ ์ธ ๊ฒฝ์šฐ
52+
if (grid[row][col] === "1") {
53+
count++;
54+
dfs(row, col);
55+
}
56+
}
57+
}
58+
59+
return count;
60+
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
*
12+
* @link https://leetcode.com/problems/reverse-linked-list/
13+
*
14+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
15+
* - ๋ฆฌ์ŠคํŠธ ์ˆœํšŒํ•˜๋ฉด์„œ ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ธฐ์กด reversed ๋ฆฌ์ŠคํŠธ์˜ head๋ฅผ ์—ฐ๊ฒฐ
16+
*
17+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
18+
* - ๋ฆฌ์ŠคํŠธ ๋…ธ๋“œ 1ํšŒ ์ˆœํšŒํ•˜๋‹ˆ๊นŒ
19+
*
20+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
21+
* - reversed ๋œ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ ์ƒˆ๋กœ ๋งŒ๋“œ๋‹ˆ๊นŒ
22+
*/
23+
24+
function reverseList(head: ListNode | null): ListNode | null {
25+
if (head === null) return head;
26+
27+
let headNode: ListNode | null = null;
28+
let currentNode: ListNode | null = head;
29+
30+
while (currentNode !== null) {
31+
const newNode = new ListNode(currentNode.val, headNode);
32+
headNode = newNode;
33+
currentNode = currentNode.next;
34+
}
35+
36+
return headNode;
37+
}

โ€Žset-matrix-zeroes/mmyeon.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
Do not return anything, modify matrix in-place instead.
3+
*/
4+
/**
5+
* @link https://leetcode.com/problems/set-matrix-zeroes/description/
6+
*
7+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
8+
* - ํ–‰๋ ฌ ์ˆœํšŒํ•˜๋ฉด์„œ 0์ด ์žˆ๋Š” ํ–‰๊ณผ ์—ด์„ rowsToZero๊ณผ colsToZero์— ์ €์žฅ
9+
* - ๋‹ค์‹œ ํ–‰๋ ฌ ์ˆœํšŒํ•˜๋ฉด์„œ rowsToZero๊ณผ colsToZero์™€ ํฌํ•จ๋œ ๊ฒฝ์šฐ 0์œผ๋กœ ๋ณ€๊ฒฝ
10+
*
11+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(m * n)
12+
* - m * n ํ–‰๋ ฌ ํฌ๊ธฐ๋งŒํผ ์ˆœํšŒ
13+
*
14+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(m + n)
15+
* - m ํ–‰๊ณผ n ์—ด ์ •๋ณด ์ €์žฅ
16+
*/
17+
function setZeroes(matrix: number[][]): void {
18+
const rows = matrix.length;
19+
const cols = matrix[0].length;
20+
const rowsToZero = new Set<number>();
21+
const colsToZero = new Set<number>();
22+
for (let row = 0; row < rows; row++) {
23+
for (let col = 0; col < cols; col++) {
24+
if (matrix[row][col] === 0) {
25+
rowsToZero.add(row);
26+
colsToZero.add(col);
27+
}
28+
}
29+
}
30+
31+
for (let row = 0; row < rows; row++) {
32+
for (let col = 0; col < cols; col++) {
33+
if (rowsToZero.has(row) || colsToZero.has(col)) {
34+
matrix[row][col] = 0;
35+
}
36+
}
37+
}
38+
}
39+
40+
// ๊ณต๊ฐ„ ๋ณต์žก๋„ O(m+n)์„ O(1)๋กœ ๊ฐœ์„ ํ•˜๊ธฐ
41+
// set์— ํ–‰๊ณผ ์—ด ์ •๋ณด ์ €์žฅํ•˜๋˜ ๋ฐฉ์‹์„ set์—†์ด ์ฒซ ๋ฒˆ์งธ ํ–‰๊ณผ ์—ด์„ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณ€๊ฒฝ
42+
function setZeroes(matrix: number[][]): void {
43+
const rows = matrix.length;
44+
const cols = matrix[0].length;
45+
let hasZeroInFirstRow = false;
46+
let hasZeroInFirstCol = false;
47+
48+
// ์ฒซ ๋ฒˆ์งธ ์—ด์— 0 ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ”Œ๋ž˜๊ทธ๋กœ ์ €์žฅ
49+
for (let row = 0; row < rows; row++) {
50+
if (matrix[row][0] === 0) hasZeroInFirstCol = true;
51+
}
52+
53+
// ์ฒซ ๋ฒˆ์งธ ํ–‰์— 0 ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ”Œ๋ž˜๊ทธ๋กœ ์ €์žฅ
54+
for (let col = 0; col < cols; col++) {
55+
if (matrix[0][col] === 0) hasZeroInFirstRow = true;
56+
}
57+
58+
// ์ฒซ ๋ฒˆ์งธ ํ–‰๊ณผ ์—ด ์ œ์™ธํ•˜๊ณ  ๋งˆ์ปค ์„ค์ •ํ•˜๊ธฐ
59+
for (let row = 1; row < rows; row++) {
60+
for (let col = 1; col < cols; col++) {
61+
if (matrix[row][col] === 0) {
62+
matrix[0][col] = 0;
63+
matrix[row][0] = 0;
64+
}
65+
}
66+
}
67+
68+
// ๋งˆ์ปค ๊ธฐ๋ฐ˜์œผ๋กœ ํ–‰๋ ฌ์— ๋ฐ˜์˜
69+
for (let row = 1; row < rows; row++) {
70+
for (let col = 1; col < cols; col++) {
71+
if (matrix[row][0] === 0 || matrix[0][col] === 0) matrix[row][col] = 0;
72+
}
73+
}
74+
75+
// ์ฒซ ๋ฒˆ์งธ ํ–‰ ์—…๋ฐ์ดํŠธ
76+
if (hasZeroInFirstRow) {
77+
for (let col = 0; col < cols; col++) {
78+
matrix[0][col] = 0;
79+
}
80+
}
81+
82+
// ์ฒซ ๋ฒˆ์งธ ์—ด ์—…๋ฐ์ดํŠธ
83+
if (hasZeroInFirstCol) {
84+
for (let row = 0; row < rows; row++) {
85+
matrix[row][0] = 0;
86+
}
87+
}
88+
}

โ€Žunique-paths/mmyeon.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @link https://leetcode.com/problems/unique-paths/
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
5+
* - ์‹œ์ž‘๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ˆ„์ ๋œ ๊ฒฝ๋กœ์˜ ์ˆ˜ ์ฐพ์•„์•ผ ํ•˜๋‹ˆ๊นŒ dp ์‚ฌ์šฉ
6+
* - ์ฒซ ๋ฒˆ์งธ ํ–‰๊ณผ, ์ฒซ ๋ฒˆ์งธ ์—ด์€ 1๋กœ ๊ณ ์ •์ด๋ผ์„œ dp ๋ฐฐ์—ด์„ 1๋กœ ์ดˆ๊ธฐํ™”ํ•จ
7+
* - ์ ํ™”์‹ : dp[x][y] = dp[x-1][y] + dp[x][y-1]
8+
*
9+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(m * n)
10+
* - m * n ํ–‰๋ ฌ ํฌ๊ธฐ๋งŒํผ ์ˆœํšŒํ•˜๋‹ˆ๊นŒ O(m * n)
11+
*
12+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(m * n)
13+
* - ์ฃผ์–ด์ง„ ํ–‰๋ ฌ ํฌ๊ธฐ๋งŒํผ dp ๋ฐฐ์—ด์— ๊ฐ’ ์ €์žฅํ•˜๋‹ˆ๊นŒ O(m * n)
14+
*/
15+
function uniquePaths(m: number, n: number): number {
16+
// m x n ๋ฐฐ์—ด ์„ ์–ธ
17+
const dp = Array.from({ length: m }, () => Array(n).fill(1));
18+
19+
for (let i = 1; i < m; i++) {
20+
for (let j = 1; j < n; j++) {
21+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
22+
}
23+
}
24+
25+
return dp[m - 1][n - 1];
26+
}
27+
28+
// ๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ O(n)์œผ๋กœ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ 1์ฐจ์› dp ๋ฐฐ์—ด ์‚ฌ์šฉ
29+
function uniquePaths(m: number, n: number): number {
30+
const rows = Array(n).fill(1);
31+
32+
for (let i = 1; i < m; i++) {
33+
for (let j = 1; j < n; j++) {
34+
rows[j] = rows[j] + rows[j - 1];
35+
}
36+
}
37+
38+
return rows[n - 1];
39+
}

0 commit comments

Comments
 (0)
Please sign in to comment.