-
Notifications
You must be signed in to change notification settings - Fork 120
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 #481 from wogha95/main
[μ¬νΈ] WEEK 07 Solutions
- Loading branch information
Showing
5 changed files
with
373 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,51 @@ | ||
/** | ||
* TC: O(S) | ||
* rightμ Sλ§νΌ μν + leftμ Sλ§νΌ μν | ||
* (κ° μνμ κ³±μ΄ μλ ν©μΈ μ΄μ λ right μν λμ leftμ μ΅λ μνκ° Sμ΄κΈ° λλ¬Έμ λλ€.) | ||
* | ||
* SC: O(S) | ||
* usedCharacterμ Sλ§νΌ λ€μ΄κ° μ μμ΅λλ€. | ||
* | ||
* S: s.length | ||
*/ | ||
|
||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var lengthOfLongestSubstring = function (s) { | ||
// 1. μ¬μ©λ λ¬Έμλ₯Ό κΈ°λ‘νκΈ° μν set | ||
const usedCharacter = new Set(); | ||
|
||
// 2. μ λ΅ μ μΆμ μν λΆλΆλ¬Έμμ΄ μ΅λ κΈΈμ΄ | ||
let maxLength = 0; | ||
|
||
// 3. μνλ₯Ό μν ν¬μΈν° + κ° indexμμ μ΅λ λ¬Έμμ΄κΈΈμ΄λ₯Ό ꡬνκΈ° μν λ³μ | ||
let left = 0; | ||
let right = 0; | ||
|
||
while (left <= right && right < s.length) { | ||
// 4. [right] λ¬Έμκ° μ¬μ©λμμΌλ©΄ | ||
if (usedCharacter.has(s[right])) { | ||
// 5. μ¬μ©λ λ¬Έμλ₯Ό λ°κ²¬νκΈ° μ κΉμ§ left μ΄λ (+ μ¬μ©λ [left] λ¬Έμ κΈ°λ‘ μ κ±°) | ||
while (s[left] !== s[right]) { | ||
usedCharacter.delete(s[left]); | ||
left += 1; | ||
} | ||
|
||
// 6. [right] λ¬Έμμ [left] λ¬Έμκ° λμΌνλ―λ‘ leftλ§ μ΄λ | ||
left += 1; | ||
} else { | ||
// 7. [right] λ¬Έμκ° λ―Έμ¬μ©λμμΌλ©΄ κΈ°λ‘ μΆκ° | ||
usedCharacter.add(s[right]); | ||
} | ||
|
||
// 8. μ€λ³΅μλ λΆλΆλ¬Έμμ΄ μ΅λ κΈΈμ΄ κ°±μ | ||
maxLength = Math.max(maxLength, right - left + 1); | ||
|
||
// 9. λ€μ λ¬Έμλ‘ μ΄λ | ||
right += 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,80 @@ | ||
/** | ||
* TC: O(ROW * COLUMN) | ||
* μ£Όμ΄μ§ grid λ°°μ΄ μ 체 μν + (μ΅μ μ κ²½μ° queueμμ grid μ 체 μν) | ||
* | ||
* SC: O(ROW * COLUMN) | ||
* queueμμ μ΅λ gridλ§νΌ μν | ||
* | ||
* ROW: grid.length, COLUMN: grid[0].length | ||
*/ | ||
|
||
/** | ||
* @param {character[][]} grid | ||
* @return {number} | ||
*/ | ||
var numIslands = function (grid) { | ||
const LAND = "1"; | ||
const VISITED_LAND = "#"; | ||
const ROW = grid.length; | ||
const COLUMN = grid[0].length; | ||
|
||
// 1. μνμ’μ° λ°©ν₯ν€ | ||
const DIRECTION = [ | ||
{ r: 0, c: 1 }, | ||
{ r: 1, c: 0 }, | ||
{ r: 0, c: -1 }, | ||
{ r: -1, c: 0 }, | ||
]; | ||
|
||
let numberOfIslands = 0; | ||
|
||
// 2. μ 체 μννλ©΄μ | ||
for (let row = 0; row < ROW; row++) { | ||
for (let column = 0; column < COLUMN; column++) { | ||
// 3. LANDλ₯Ό λ°κ²¬νλ©΄ λ°©λ¬Έν μ¬μΌλ‘ νμ(bfs)νκ³ μ¬κ°―μ κ°±μ | ||
if (grid[row][column] === LAND) { | ||
bfs(row, column); | ||
numberOfIslands += 1; | ||
} | ||
} | ||
} | ||
|
||
return numberOfIslands; | ||
|
||
function bfs(startRow, startColumn) { | ||
// 4. μμμ’ν queueμ λ£κ³ λ°©λ¬Έ νμ | ||
const queue = [[startRow, startColumn]]; | ||
grid[startRow][startColumn] = VISITED_LAND; | ||
|
||
while (queue.length > 0) { | ||
const [row, column] = queue.shift(); | ||
|
||
// 5. μνμ’μ°μ μ’νλ₯Ό κ°μ§κ³ | ||
for (const direction of DIRECTION) { | ||
const nextRow = row + direction.r; | ||
const nextColumn = column + direction.c; | ||
|
||
// 6. μ ν¨ν μ’ν && λ―Έλ°©λ¬Έ μ‘μ§μΈμ§ νμΈ | ||
if ( | ||
isValidPosition(nextRow, nextColumn) && | ||
grid[nextRow][nextColumn] === LAND | ||
) { | ||
// 7. queueμ μΆκ°νκ³ λ°©λ¬Έ νμ | ||
grid[nextRow][nextColumn] = VISITED_LAND; | ||
queue.push([nextRow, nextColumn]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 8. μ£Όμ΄μ§ 2μ°¨μ λ°°μ΄μ μ ν¨ν μ’νμΈμ§ νμΈνλ ν¨μ | ||
function isValidPosition(row, column) { | ||
if (row < 0 || ROW <= row) { | ||
return false; | ||
} | ||
if (column < 0 || COLUMN <= column) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
}; |
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,69 @@ | ||
/** | ||
* 2μ°¨ | ||
* Tonyλ νμ΄ μ°Έκ³ ν΄μ SC κ°μ | ||
* | ||
* TC: O(N) | ||
* SC: O(1) | ||
* μνλμ λ Έλ μμ±νμ§ μμΌλ―λ‘ κ³΅κ°λ³΅μ‘λκ° μμλ€. | ||
* | ||
* N: linked-list length | ||
*/ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var reverseList = function (head) { | ||
let pointer = null; | ||
|
||
while (head !== null) { | ||
let temp = head.next; | ||
head.next = pointer; | ||
pointer = head; | ||
head = temp; | ||
} | ||
|
||
return pointer; | ||
}; | ||
|
||
/** | ||
* 1μ°¨ | ||
* TC: O(N) | ||
* linked-list κΈΈμ΄ λ§νΌ μν | ||
* | ||
* SC: O(N) | ||
* linked-list κΈΈμ΄λ§νΌ μμ± | ||
* | ||
* N: linked-list length | ||
*/ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var reverseList = function (head) { | ||
let pointer = null; | ||
|
||
while (head) { | ||
// 1. μ λ΅ λ¦¬μ€νΈμ 맨 μμ μλ‘μ΄ λ Έλλ₯Ό μΆκ° | ||
pointer = new ListNode(head.val, pointer); | ||
// 2. headλ λ€μ λ Έλλ‘ μ΄λ | ||
head = head.next; | ||
} | ||
|
||
return pointer; | ||
}; |
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,46 @@ | ||
/** | ||
* TC: O(ROW * COLUMN) | ||
* SC: O(1) | ||
*/ | ||
|
||
/** | ||
* @param {number[][]} matrix | ||
* @return {void} Do not return anything, modify matrix in-place instead. | ||
*/ | ||
var setZeroes = function (matrix) { | ||
const ROW = matrix.length; | ||
const COLUMN = matrix[0].length; | ||
const MARK = "#"; | ||
|
||
// 1. 0μΈ μμμ κ°λ‘, μΈλ‘λ₯Ό νΉμ λ¬Έμλ‘ λ³κ²½ | ||
for (let row = 0; row < ROW; row++) { | ||
for (let column = 0; column < COLUMN; column++) { | ||
if (matrix[row][column] === 0) { | ||
changeToMark(row, column); | ||
} | ||
} | ||
} | ||
|
||
// 2. νΉμ λ¬Έμλ₯Ό λͺ¨λ 0μΌλ‘ λ³κ²½ | ||
for (let row = 0; row < ROW; row++) { | ||
for (let column = 0; column < COLUMN; column++) { | ||
if (matrix[row][column] === MARK) { | ||
matrix[row][column] = 0; | ||
} | ||
} | ||
} | ||
|
||
// 3. νΉμ μ’νμ κ°λ‘, μΈλ‘λ₯Ό charλ¬Έμλ‘ λ³κ²½ (λμ 0μΈ μμλ λ³κ²½νμ§ μμ) | ||
function changeToMark(row, column) { | ||
for (let r = 0; r < ROW; r++) { | ||
if (matrix[r][column] !== 0) { | ||
matrix[r][column] = MARK; | ||
} | ||
} | ||
for (let c = 0; c < COLUMN; c++) { | ||
if (matrix[row][c] !== 0) { | ||
matrix[row][c] = MARK; | ||
} | ||
} | ||
} | ||
}; |
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,127 @@ | ||
/** | ||
* 3μ°¨ (μκ°, κ³΅κ° λ³΅μ‘λ κ°μ ) | ||
* λμΌν downλ°©ν₯, rightλ°©ν₯λ€ μ€μμ λμ΄νλ λ°©λ² | ||
* μ¦, ((m - 1) + (n - 1))! / ((m - 1)! * (n - 1)!) | ||
* (+ ν©ν 리μΌμ μλ λ§€μ° λΉ λ₯΄κ² 컀μ§λ―λ‘ μ€κ° λλμ μ΄ κ°λ₯ν λλ§λ€ λλμ΄μ integer λ²μλ₯Ό λμ§ μλλ‘ λ°©μ§) | ||
* | ||
* TC: O(M + N) | ||
* 1λΆν° μ΅λ (M - 1) + (N - 1)κΉμ§ μν | ||
* | ||
* SC: O(1) | ||
* κ³μ°μ κ²°κ³Ό λ³μκ° m, nκ³Ό 무κ΄νλ―λ‘ μμμ 곡κ°λ³΅μ‘λ | ||
*/ | ||
|
||
/** | ||
* @param {number} m | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var uniquePaths = function (m, n) { | ||
// 1. downλ°©ν₯, rightλ°©ν₯μ μ | ||
const NUMBER_OF_DOWN = m - 1; | ||
const NUMBER_OF_RIGHT = n - 1; | ||
|
||
// 2. factorial κ³μ°μ μν λ³μ | ||
let result = 1; | ||
let factorialOfDown = 1; | ||
let factorialOfRight = 1; | ||
|
||
// 3. 'downλ°©ν₯ μ + rightλ°©ν₯ μ'λ§νΌ μννλ©΄μ | ||
for (let number = 1; number <= NUMBER_OF_DOWN + NUMBER_OF_RIGHT; number++) { | ||
result *= number; | ||
|
||
// 4. factorial κ°λ€μ΄ 컀μ§μ§ μλλ‘ λλμ μμλλ§λ€ λλ (factorial of down) | ||
if (number <= NUMBER_OF_DOWN) { | ||
factorialOfDown *= number; | ||
if (result % factorialOfDown === 0) { | ||
result /= factorialOfDown; | ||
factorialOfDown = 1; | ||
} | ||
} | ||
|
||
// 5. factorial κ°λ€μ΄ 컀μ§μ§ μλλ‘ λλμ μμλλ§λ€ λλ (factorial of right) | ||
if (number <= NUMBER_OF_RIGHT) { | ||
factorialOfRight *= number; | ||
if (result % factorialOfRight === 0) { | ||
result /= factorialOfRight; | ||
factorialOfRight = 1; | ||
} | ||
} | ||
} | ||
|
||
return result / factorialOfDown / factorialOfRight; | ||
}; | ||
|
||
/** | ||
* 2μ°¨ (곡κ°λ³΅μ‘λ κ°μ ) | ||
* μ΄μ νμ΄μμ λͺ¨λ νμ κ²½λ‘μλ₯Ό κΈ°μ΅ν νμκ° μλ μ μ νμ© | ||
* | ||
* TC: O(M * N) | ||
* κ²½λ‘ μλ₯Ό κΈ°λ‘νκΈ° μν Nλ°°μ΄ μν * (M - 1) | ||
* | ||
* SC: O(N) | ||
* κ²½λ‘μ κΈ°λ‘μ μν 1μ°¨μ λ°°μ΄ | ||
*/ | ||
|
||
/** | ||
* @param {number} m | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var uniquePaths = function (m, n) { | ||
// 1. μ΅μλ¨μ κ²½λ‘μλ λͺ¨λ 1 | ||
const numberOfPaths = new Array(n).fill(1); | ||
|
||
for (let row = 1; row < m; row++) { | ||
// 2. κ° μ’νμ κ²½λ‘μλ νμ’ν(1μ°¨ νμ΄μ row-1)μ μ’μΈ‘μ’ν(1μ°¨ νμ΄μ column-1)μ ν© | ||
for (let column = 1; column < n; column++) { | ||
numberOfPaths[column] += numberOfPaths[column - 1]; | ||
} | ||
} | ||
|
||
return numberOfPaths[n - 1]; | ||
}; | ||
|
||
/** | ||
* 1μ°¨ | ||
* κ° μ’νμ κ²½λ‘μλ₯Ό κΈ°λ‘νμ¬ dpλ‘ νμ΄ | ||
* νμ’νκΉμ§μ κ²½λ‘μ = μλ¨μ’νμμ μ¨ κ²½μ° + μ’μΈ‘μ’νμμ μ¨ κ²½μ° | ||
* dp[row][column] = dp[row - 1][column] + dp[row][column - 1] | ||
* | ||
* | ||
* TC: O(M * N) | ||
* κ²½λ‘μλ₯Ό κΈ°λ‘ν 2μ°¨μ λ°°μ΄μ μ 체 μν | ||
* | ||
* SC: O(M * N) | ||
* κ²½λ‘μ κΈ°λ‘μ μν 2μ°¨μ λ°°μ΄ | ||
*/ | ||
|
||
/** | ||
* @param {number} m | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var uniquePaths = function (m, n) { | ||
// 1. κ° μ’λ£κΉμ§μ κ²½λ‘μλ₯Ό κΈ°λ‘νκΈ° μν λ°°μ΄ | ||
const numberOfPaths = new Array(m).fill(new Array(n).fill(0)); | ||
|
||
// 2. μ΅μ’μΈ‘μ μλ μ’νμ κ²½λ‘μλ 1 | ||
for (let row = 0; row < m; row++) { | ||
numberOfPaths[row][0] = 1; | ||
} | ||
|
||
// 3. μ΅μλ¨μ μλ μ’νμ κ²½λ‘μλ 1 | ||
for (let column = 0; column < n; column++) { | ||
numberOfPaths[0][column] = 1; | ||
} | ||
|
||
// 4. κ·Έ μΈ κ° μ’νλ λ°λ‘ μ μ’ν(column-1)μ λ°λ‘ μΌμͺ½ μ’ν(row-1)μ κ²½λ‘μμ ν© | ||
for (let row = 1; row < m; row++) { | ||
for (let column = 1; column < n; column++) { | ||
numberOfPaths[row][column] = | ||
numberOfPaths[row - 1][column] + numberOfPaths[row][column - 1]; | ||
} | ||
} | ||
|
||
return numberOfPaths[m - 1][n - 1]; | ||
}; |