-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #941 from mmyeon/main
[mallayon] Week 7
- Loading branch information
Showing
5 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
*@link https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ | ||
* | ||
* ์ ๊ทผ ๋ฐฉ๋ฒ : | ||
* - ์ฌ๋ผ์ฐ๋ฉ ์๋์ฐ์ set ์ฌ์ฉํด์ ์ค๋ณต์๋ ๋ฌธ์์ด ํ์ธ | ||
* - ์ค๋ณต ๋ฌธ์ ๋ฐ๊ฒฌํ๋ฉด ์๋์ฐ ์ถ์ | ||
* | ||
* ์๊ฐ๋ณต์ก๋ : O(n) | ||
* - ๊ฐ ๋ฌธ์ ์ํํ๋๊น | ||
* | ||
* ๊ณต๊ฐ๋ณต์ก๋ : O(n) | ||
* - ์ค๋ณต ์๋ ๊ฒฝ์ฐ ์ต๋ n๊ฐ์ ๋ฌธ์ set์ ์ ์ฅ | ||
*/ | ||
function lengthOfLongestSubstring(s: string): number { | ||
let start = 0, | ||
end = 0, | ||
maxLength = 0; | ||
const set = new Set<string>(); | ||
|
||
while (end < s.length) { | ||
const char = s[end]; | ||
|
||
// ์ค๋ณต์ด ์์ผ๋ฉด ์๋์ฐ ์ถ์ | ||
if (set.has(char)) { | ||
set.delete(s[start]); | ||
start++; | ||
} else { | ||
// ์ค๋ณต ์์ผ๋ฉด set์ ๋ฌธ์ ์ถ๊ฐ, ์๋์ฐ ํ์ฅ | ||
set.add(char); | ||
maxLength = Math.max(maxLength, end - start + 1); | ||
end++; | ||
} | ||
} | ||
|
||
return maxLength; | ||
} | ||
|
||
// Map ์ฌ์ฉํด์ ์ค๋ณต ๋ฐ์์ start ์ธ๋ฑ์ค๊ฐ ์ ํํ๋๋ก ๊ฐ์ | ||
function lengthOfLongestSubstring(s: string): number { | ||
let start = 0, | ||
maxLength = 0; | ||
const map = new Map<string, number>(); | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
const char = s[i]; | ||
// ์ค๋ณต ์๋ ๊ฒฝ์ฐ, ์ค๋ณต๋ฌธ์์ ๋ค์ ์์น๋ก ์ ํ | ||
if (map.has(char)) start = Math.max(start, map.get(char)! + 1); | ||
// ์ธ๋ฑ์ค ๊ฐฑ์ | ||
map.set(char, i); | ||
|
||
maxLength = Math.max(maxLength, i - start + 1); | ||
} | ||
|
||
return maxLength; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
*@link https://leetcode.com/problems/number-of-islands/ | ||
* | ||
* ์ ๊ทผ ๋ฐฉ๋ฒ : | ||
* - ์ฌ์ ์์์ ์์ ๋๊น์ง ํ์ํด์ผ ํ๋ฏ๋ก DFS ์ฌ์ฉ | ||
* - ๋ฐฉ๋ฌธํ ์ฌ์ ์ค๋ณต ํ์ ๋ฐฉ์งํ๊ธฐ ์ํด์ ๋ฐฉ๋ฌธ ํ ๊ฐ ๋ณ๊ฒฝ ์ฒ๋ฆฌ | ||
* | ||
* ์๊ฐ๋ณต์ก๋ : O(m * n) | ||
* - 2์ฐจ์ ๋ฐฐ์ด ์ ์ฒด๋ฅผ ์ํํ๋ฏ๋ก O(m * n) | ||
* | ||
* ๊ณต๊ฐ๋ณต์ก๋ : O(m * n) | ||
* - DFS ํธ์ถ ์คํ ์ต๋ ๊น์ด๊ฐ m * n ๋งํผ ์์ผ ์ ์๋ค. | ||
*/ | ||
|
||
function numIslands(grid: string[][]): number { | ||
let count = 0; | ||
const rows = grid.length; | ||
const cols = grid[0].length; | ||
|
||
// ์ํ์ข์ฐ ํ์ ๋ฐฉํฅ ๋ฐฐ์ด | ||
const directions = [ | ||
[0, -1], | ||
[0, 1], | ||
[-1, 0], | ||
[1, 0], | ||
]; | ||
|
||
// ํ์ฌ ์์น์์ ์ฐ๊ฒฐ๋ ๋ชจ๋ ์ฌ ํ์ | ||
const dfs = (row: number, col: number) => { | ||
// ์ข ๋ฃ ์กฐ๊ฑด : ๋ฒ์ ๋ฒ์ด๋๊ฑฐ๋, ์ด๋ฏธ ๋ฐฉ๋ฌธํ ๊ฒฝ์ฐ | ||
if ( | ||
row < 0 || | ||
row >= rows || | ||
col < 0 || | ||
col >= cols || | ||
grid[row][col] === "0" | ||
) | ||
return; | ||
|
||
// ํ์ฌ ์์น ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ | ||
grid[row][col] = "0"; | ||
|
||
// ์ํ์ข์ฐ ํ์ | ||
for (const [x, y] of directions) { | ||
dfs(row + x, col + y); | ||
} | ||
}; | ||
|
||
for (let row = 0; row < rows; row++) { | ||
for (let col = 0; col < cols; col++) { | ||
// ์ฌ์ ์์์ ์ธ ๊ฒฝ์ฐ | ||
if (grid[row][col] === "1") { | ||
count++; | ||
dfs(row, col); | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class ListNode { | ||
val: number; | ||
next: ListNode | null; | ||
constructor(val?: number, next?: ListNode | null) { | ||
this.val = val === undefined ? 0 : val; | ||
this.next = next === undefined ? null : next; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @link https://leetcode.com/problems/reverse-linked-list/ | ||
* | ||
* ์ ๊ทผ ๋ฐฉ๋ฒ : | ||
* - ๋ฆฌ์คํธ ์ํํ๋ฉด์ ์๋ก์ด ๋ ธ๋๋ฅผ ์์ฑํ๊ณ ๊ธฐ์กด reversed ๋ฆฌ์คํธ์ head๋ฅผ ์ฐ๊ฒฐ | ||
* | ||
* ์๊ฐ๋ณต์ก๋ : O(n) | ||
* - ๋ฆฌ์คํธ ๋ ธ๋ 1ํ ์ํํ๋๊น | ||
* | ||
* ๊ณต๊ฐ๋ณต์ก๋ : O(n) | ||
* - reversed ๋ ๋งํฌ๋ ๋ฆฌ์คํธ ์๋ก ๋ง๋๋๊น | ||
*/ | ||
|
||
function reverseList(head: ListNode | null): ListNode | null { | ||
if (head === null) return head; | ||
|
||
let headNode: ListNode | null = null; | ||
let currentNode: ListNode | null = head; | ||
|
||
while (currentNode !== null) { | ||
const newNode = new ListNode(currentNode.val, headNode); | ||
headNode = newNode; | ||
currentNode = currentNode.next; | ||
} | ||
|
||
return headNode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
Do not return anything, modify matrix in-place instead. | ||
*/ | ||
/** | ||
* @link https://leetcode.com/problems/set-matrix-zeroes/description/ | ||
* | ||
* ์ ๊ทผ ๋ฐฉ๋ฒ : | ||
* - ํ๋ ฌ ์ํํ๋ฉด์ 0์ด ์๋ ํ๊ณผ ์ด์ rowsToZero๊ณผ colsToZero์ ์ ์ฅ | ||
* - ๋ค์ ํ๋ ฌ ์ํํ๋ฉด์ rowsToZero๊ณผ colsToZero์ ํฌํจ๋ ๊ฒฝ์ฐ 0์ผ๋ก ๋ณ๊ฒฝ | ||
* | ||
* ์๊ฐ๋ณต์ก๋ : O(m * n) | ||
* - m * n ํ๋ ฌ ํฌ๊ธฐ๋งํผ ์ํ | ||
* | ||
* ๊ณต๊ฐ๋ณต์ก๋ : O(m + n) | ||
* - m ํ๊ณผ n ์ด ์ ๋ณด ์ ์ฅ | ||
*/ | ||
function setZeroes(matrix: number[][]): void { | ||
const rows = matrix.length; | ||
const cols = matrix[0].length; | ||
const rowsToZero = new Set<number>(); | ||
const colsToZero = new Set<number>(); | ||
for (let row = 0; row < rows; row++) { | ||
for (let col = 0; col < cols; col++) { | ||
if (matrix[row][col] === 0) { | ||
rowsToZero.add(row); | ||
colsToZero.add(col); | ||
} | ||
} | ||
} | ||
|
||
for (let row = 0; row < rows; row++) { | ||
for (let col = 0; col < cols; col++) { | ||
if (rowsToZero.has(row) || colsToZero.has(col)) { | ||
matrix[row][col] = 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// ๊ณต๊ฐ ๋ณต์ก๋ O(m+n)์ O(1)๋ก ๊ฐ์ ํ๊ธฐ | ||
// set์ ํ๊ณผ ์ด ์ ๋ณด ์ ์ฅํ๋ ๋ฐฉ์์ set์์ด ์ฒซ ๋ฒ์งธ ํ๊ณผ ์ด์ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋ณ๊ฒฝ | ||
function setZeroes(matrix: number[][]): void { | ||
const rows = matrix.length; | ||
const cols = matrix[0].length; | ||
let hasZeroInFirstRow = false; | ||
let hasZeroInFirstCol = false; | ||
|
||
// ์ฒซ ๋ฒ์งธ ์ด์ 0 ์๋์ง ์ฌ๋ถ๋ฅผ ํ๋๊ทธ๋ก ์ ์ฅ | ||
for (let row = 0; row < rows; row++) { | ||
if (matrix[row][0] === 0) hasZeroInFirstCol = true; | ||
} | ||
|
||
// ์ฒซ ๋ฒ์งธ ํ์ 0 ์๋์ง ์ฌ๋ถ๋ฅผ ํ๋๊ทธ๋ก ์ ์ฅ | ||
for (let col = 0; col < cols; col++) { | ||
if (matrix[0][col] === 0) hasZeroInFirstRow = true; | ||
} | ||
|
||
// ์ฒซ ๋ฒ์งธ ํ๊ณผ ์ด ์ ์ธํ๊ณ ๋ง์ปค ์ค์ ํ๊ธฐ | ||
for (let row = 1; row < rows; row++) { | ||
for (let col = 1; col < cols; col++) { | ||
if (matrix[row][col] === 0) { | ||
matrix[0][col] = 0; | ||
matrix[row][0] = 0; | ||
} | ||
} | ||
} | ||
|
||
// ๋ง์ปค ๊ธฐ๋ฐ์ผ๋ก ํ๋ ฌ์ ๋ฐ์ | ||
for (let row = 1; row < rows; row++) { | ||
for (let col = 1; col < cols; col++) { | ||
if (matrix[row][0] === 0 || matrix[0][col] === 0) matrix[row][col] = 0; | ||
} | ||
} | ||
|
||
// ์ฒซ ๋ฒ์งธ ํ ์ ๋ฐ์ดํธ | ||
if (hasZeroInFirstRow) { | ||
for (let col = 0; col < cols; col++) { | ||
matrix[0][col] = 0; | ||
} | ||
} | ||
|
||
// ์ฒซ ๋ฒ์งธ ์ด ์ ๋ฐ์ดํธ | ||
if (hasZeroInFirstCol) { | ||
for (let row = 0; row < rows; row++) { | ||
matrix[row][0] = 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @link https://leetcode.com/problems/unique-paths/ | ||
* | ||
* ์ ๊ทผ ๋ฐฉ๋ฒ : | ||
* - ์์๋ถํฐ ๋๊น์ง ๋์ ๋ ๊ฒฝ๋ก์ ์ ์ฐพ์์ผ ํ๋๊น dp ์ฌ์ฉ | ||
* - ์ฒซ ๋ฒ์งธ ํ๊ณผ, ์ฒซ ๋ฒ์งธ ์ด์ 1๋ก ๊ณ ์ ์ด๋ผ์ dp ๋ฐฐ์ด์ 1๋ก ์ด๊ธฐํํจ | ||
* - ์ ํ์ : dp[x][y] = dp[x-1][y] + dp[x][y-1] | ||
* | ||
* ์๊ฐ๋ณต์ก๋ : O(m * n) | ||
* - m * n ํ๋ ฌ ํฌ๊ธฐ๋งํผ ์ํํ๋๊น O(m * n) | ||
* | ||
* ๊ณต๊ฐ๋ณต์ก๋ : O(m * n) | ||
* - ์ฃผ์ด์ง ํ๋ ฌ ํฌ๊ธฐ๋งํผ dp ๋ฐฐ์ด์ ๊ฐ ์ ์ฅํ๋๊น O(m * n) | ||
*/ | ||
function uniquePaths(m: number, n: number): number { | ||
// m x n ๋ฐฐ์ด ์ ์ธ | ||
const dp = Array.from({ length: m }, () => Array(n).fill(1)); | ||
|
||
for (let i = 1; i < m; i++) { | ||
for (let j = 1; j < n; j++) { | ||
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; | ||
} | ||
} | ||
|
||
return dp[m - 1][n - 1]; | ||
} | ||
|
||
// ๊ณต๊ฐ ๋ณต์ก๋๋ฅผ O(n)์ผ๋ก ์ต์ ํํ๊ธฐ ์ํด์ 1์ฐจ์ dp ๋ฐฐ์ด ์ฌ์ฉ | ||
function uniquePaths(m: number, n: number): number { | ||
const rows = Array(n).fill(1); | ||
|
||
for (let i = 1; i < m; i++) { | ||
for (let j = 1; j < n; j++) { | ||
rows[j] = rows[j] + rows[j - 1]; | ||
} | ||
} | ||
|
||
return rows[n - 1]; | ||
} |