-
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 #829 from Yjason-K/main
[gomgom22] Week4
- Loading branch information
Showing
5 changed files
with
242 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,46 @@ | ||
/** | ||
* ๊ฐ์ง๊ณ ์๋ ๋์ ์ ์ต๋ํ ํ์ฉํ์ฌ ์ต์์ ์กฐํฉ์ผ๋ก amount๋ฅผ ๋ง๋๋ ์ต์ ๋์ ๊ฐ์ ๊ตฌํ๋ ํจ์ | ||
* | ||
* @param {number[]} coins - ์ฌ์ฉ ๊ฐ๋ฅํ ๋์ ๋ฐฐ์ด | ||
* @param {number} amount - ๋ง๋ค์ด์ผ ํ๋ ์ดํฉ | ||
* @returns {number} | ||
* | ||
* ์๊ฐ ๋ณต์ก๋ O(n * m) | ||
* - n์ ๋์ ๋ฐฐ์ด์ ํฌ๊ธฐ | ||
* - m์ amount | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋ (n); | ||
* - ํ์ ์ต๋ n๊ฐ์ ์์๊ฐ ๋ค์ด๊ฐ ์ ์์ | ||
*/ | ||
function coinChange(coins: number[], amount: number): number { | ||
// ์ดํฉ์ด 0์ธ ๊ฒฝ์ฐ 0 ๋ฐํ | ||
if (amount === 0) return 0; | ||
|
||
// ๋๋น ์ฐ์ ํ์์ ํ์ฉํ ํ์ด | ||
|
||
const queue: [number, number] [] = [[0, 0]]; // [ํ์ฌ ์ดํฉ, ๊น์ด] | ||
const visited = new Set<number>(); | ||
|
||
while (queue.length > 0) { | ||
const [currentSum, depth] = queue.shift()!; | ||
|
||
// ๋์ ์ ํ๋์ฉ ๋ํด์ ๋ค์ ๊น์ด์ ํ์ | ||
for (const coin of coins) { | ||
const nextSum = currentSum + coin; | ||
|
||
// ๋ชฉํ ๊ธ์ก์ ๋๋ฌํ๋ฉด ํ์ฌ ๊น์ด๋ฅผ ๋ฐํ | ||
if (nextSum === amount) return depth + 1; | ||
|
||
// ์์ง ์ดํฉ์ ๋๋ฌํ์ง ์์๊ณ , ์ค๋ณต๋์ง ์์ ํ์ ๊ฐ๋ฅํ ๊ฒฝ์ฐ | ||
if (nextSum < amount && !visited.has(nextSum)) { | ||
queue.push([nextSum, depth + 1]); | ||
visited.add(nextSum) | ||
} | ||
|
||
} | ||
} | ||
|
||
// ํ์ ์กฐ๊ฑด์ ์๋ฃ ํด๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฐพ์ง ๋ชปํ ๊ฒฝ์ฐ | ||
return -1; | ||
} | ||
|
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,49 @@ | ||
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) | ||
} | ||
} | ||
|
||
/** | ||
* ๋ ๊ฐ์ ์ ๋ ฌ๋ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ๋ณํฉํ์ฌ ํ๋์ ์ ๋ ฌ๋ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๋ ํจ์ | ||
* @param {ListNode | null} list1 - ์ฒซ ๋ฒ์งธ ์ ๋ ฌ๋ ์ฐ๊ฒฐ ๋ฆฌ์คํธ | ||
* @param {ListNode | null} list2 - ๋ ๋ฒ์งธ ์ ๋ ฌ๋ ์ฐ๊ฒฐ ๋ฆฌ์คํธ | ||
* @returns {ListNode | null} - ๋ณํฉ๋ ์ ๋ ฌ๋ ์ฐ๊ฒฐ ๋ฆฌ์คํธ | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(n + m) | ||
* - n: list1์ ๋ ธ๋ ๊ฐ์ | ||
* - m: list2์ ๋ ธ๋ ๊ฐ์ | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
* - ์๋ก์ด ๋ ธ๋๋ฅผ ์์ฑํ์ง ์๊ณ ๊ธฐ์กด ๋ ธ๋์ ํฌ์ธํฐ๋ฅผ ์กฐ์ ํ์ฌ ๋ณํฉํ๋ฏ๋ก ์ถ๊ฐ ๊ณต๊ฐ ์ฌ์ฉ ์์ | ||
*/ | ||
|
||
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { | ||
// null ์์ธ ์ฒ๋ฆฌ | ||
if (!list1 || !list2) return list1 || list2; | ||
|
||
// ์์์ ์ ์ํ ๋๋ฏธ ํค๋ ๋ ธ๋ ์์ฑ | ||
const start = new ListNode(-101); | ||
let current = start; | ||
|
||
// ๋ ๋ฆฌ์คํธ๋ฅผ ์์ฐจ์ ์ผ๋ก ๋น๊ตํ๋ฉฐ ๋ณํฉ | ||
while (list1 !== null && list2 !== null) { | ||
if (list1.val <= list2.val) { | ||
current.next = list1; | ||
list1 = list1.next; | ||
} else { | ||
current.next = list2; | ||
list2 = list2.next; | ||
} | ||
current = current.next; | ||
} | ||
|
||
// ๋จ์ ๋ ธ๋๋ฅผ ์ฐ๊ฒฐ (list1 ๋๋ list2 ์ค ํ๋๋ null ์ํ) | ||
current.next = list1 !== null ? list1 : list2; | ||
|
||
// ๋๋ฏธ ํค๋์ ๋ค์ ๋ ธ๋๊ฐ ์ค์ ๋ณํฉ๋ ๋ฆฌ์คํธ์ ์์ | ||
return start.next; | ||
} |
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,29 @@ | ||
/** | ||
* 0๋ถํฐ n๊น์ง์ ์ซ์ ์ค ๋ฐฐ์ด์์ ๋๋ฝ๋ ์ซ์๋ฅผ ์ฐพ๋ ํจ์ | ||
* @param {number[]} nums - 0๋ถํฐ n๊น์ง์ ์ซ์๊ฐ ํฌํจ๋ ๋ฐฐ์ด (์์๋ ๋ฌด์์์ด๋ฉฐ ์ผ๋ถ ์ซ์๊ฐ ๋๋ฝ๋ ์ ์์) | ||
* @returns {number} - ๋ฐฐ์ด์์ ๋๋ฝ๋ ์ซ์ | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(n) | ||
* - Set์ Hash Table๋ก ๊ตฌํ๋์ด has ๋ฉ์๋๊ฐ Array.includes ๋ฉ์๋๋ณด๋ค ์ ๋ฆฌ | ||
* - Set์ ์์ฑํ๋ ๋ฐ O(n) ์๊ฐ์ด ์์ | ||
* - ๋ฐฐ์ด ๊ธธ์ด๋งํผ ๋ฐ๋ณต๋ฌธ์ ๋๋ฉด์ Set์ ์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ๋ฐ O(1) * n = O(n) | ||
* - ๊ฒฐ๊ณผ์ ์ผ๋ก O(n) + O(n) = O(n) | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(n) | ||
* - Set ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ ฅ ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ์ ์ฅํ๋ฏ๋ก O(n)์ ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ | ||
*/ | ||
function missingNumber(nums: number[]): number { | ||
// ๋ฐฐ์ด์ ์ซ์๋ฅผ ๋ชจ๋ Set์ ์ถ๊ฐํ์ฌ ์ค๋ณต ์ ๊ฑฐ | ||
const distinctSet = new Set([...nums]); | ||
|
||
// 0๋ถํฐ n๊น์ง์ ์ซ์ ์ค์์ ๋๋ฝ๋ ์ซ์๋ฅผ ํ์ | ||
for (let i = 0; i < nums.length; i++) { | ||
// Set์ i๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด i๊ฐ ๋๋ฝ๋ ์ซ์ | ||
if (!distinctSet.has(i)) { | ||
return i; | ||
} | ||
} | ||
|
||
// ๋ชจ๋ ์ซ์๊ฐ Set์ ์กด์ฌํ๋ฉด n์ด ๋๋ฝ๋ ์ซ์ | ||
return nums.length; | ||
} |
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,48 @@ | ||
/** | ||
* ๋ฌธ์์ด์์ substring ์ค panlindrome์ธ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ตฌํ๋ ํจ์ | ||
* @param {string} s - ์ ๋ ฅ ๋ฌธ์์ด | ||
* @returns {number} - s๋ฌธ์์ด์์ ์ฐพ์์ ์๋ panlindrome substring์ ๊ฐ์ | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(n^2) (n: ๋ฌธ์์ด ๊ธธ์ด) | ||
* - ํ ๋ฒ์ ์ธ๋ถ ๋ฃจํ: ๋ถ๋ถ ๋ฌธ์์ด ๊ธธ์ด (\(subLen\)) - O(n) | ||
* - ๋ด๋ถ ๋ฃจํ: ์์ ์ธ๋ฑ์ค (\(start\)) - O(n) | ||
* - ๋ฐ๋ผ์, ์ด ๋ณต์ก๋๋ O(n^2) | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(n^2) | ||
* - DP ๋ฐฐ์ด dp[i][j]๋ \(n^2\) ํฌ๊ธฐ๋ฅผ ๊ฐ์ง๋ฉฐ ๋ฌธ์์ด์ ๋ชจ๋ ์์๊ณผ ๋ ์กฐํฉ์ ๋ํ ์ ๋ณด๋ฅผ ์ ์ฅํฉ๋๋ค. | ||
*/ | ||
function countSubstrings(s: string): number { | ||
const n = s.length; // ๋ฌธ์์ด ๊ธธ์ด | ||
let result = 0; | ||
|
||
// DP ๋ฐฐ์ด ์์ฑ (dp[i][j] ๋ s[i] ~ s[j] ๊น์ง๊ฐ ํ๋ฌธ์ธ์ง ์ฌ๋ถ๋ฅผ ์ ์ฅ) | ||
const dp: boolean[][] = Array.from({ length: n }, () => Array(n).fill(false)); | ||
|
||
// 1๊ธ์ ๊ฒฝ์ฐ | ||
for (let i = 0; i < n; i++) { | ||
dp[i][i] = true; | ||
result++; | ||
} | ||
|
||
// 2๊ธ์ ๊ฒฝ์ฐ | ||
for (let i = 0; i < n - 1; i++) { | ||
if (s[i] === s[i + 1]) { | ||
dp[i][i + 1] = true; | ||
result++; | ||
} | ||
} | ||
|
||
// 3๊ธ์ ์ด์์ธ ๊ฒฝ์ฐ | ||
for (let subLen = 3; subLen <= n; subLen++) { | ||
for (let start = 0; start <= n - subLen; start++) { | ||
const end = start + subLen - 1; | ||
// ์ ๋ ๋ฌธ์๊ฐ ๊ฐ๊ณ , ๋ด๋ถ ๋ถ๋ถ ๋ฌธ์์ด์ด ํ๋ฌธ์ด๋ฉด true | ||
if (s[start] === s[end] && dp[start + 1][end - 1]) { | ||
dp[start][end] = true; | ||
result++; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} |
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,70 @@ | ||
/** | ||
* board ์์ ์ฃผ์ด์ง ๋จ์ด๋ฅผ ์ฐพ์ ์ ์๋์ง ์ฌ๋ถ ํ์ธ (boolean) | ||
* @param {string[][]} board - ๋จ์ด๋ฅผ ํ์ํ 2D board | ||
* @param {string} word - ์ฐพ๊ณ ์ ํ๋ ๋จ์ด | ||
* @returns {boolean} - ๋จ์ด๊ฐ ๊ฒฉ์์์ ์กด์ฌํ๋ฉด true, ๊ทธ๋ ์ง ์์ผ๋ฉด false | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(N * M * 4^L) | ||
* - N: board์ ํ ๊ฐ์ | ||
* - M: board์ ์ด ๊ฐ์ | ||
* - L: word์ ๊ธธ์ด | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(L) (์ฌ๊ท ํธ์ถ ์คํ) | ||
*/ | ||
function exist(board: string[][], word: string): boolean { | ||
const rows = board.length; | ||
const cols = board[0].length; | ||
|
||
// ๋ฐฉํฅ ๋ฐฐ์ด (์, ํ, ์ข, ์ฐ) | ||
const directions = [ | ||
[0, -1], // ์ | ||
[0, 1], // ํ | ||
[-1, 0], // ์ข | ||
[1, 0], // ์ฐ | ||
]; | ||
|
||
/** | ||
* DFS ํ์(๊น์ด ์ฐ์ ํ์)์ ํตํด ๋จ์ด๋ฅผ ์ฐพ๋ ํจ์ | ||
* @param {number} x - ํ์ฌ x ์ขํ (์ด) | ||
* @param {number} y - ํ์ฌ y ์ขํ (ํ) | ||
* @param {number} index - ํ์ฌ ํ์ ์ค์ธ word์ ๋ฌธ์ ์ธ๋ฑ์ค | ||
* @returns {boolean} - ํ์ฌ ๊ฒฝ๋ก๊ฐ ์ ํจํ๋ฉด true, ์ ํจํ์ง ์์ผ๋ฉด false | ||
*/ | ||
const dfs = (x: number, y: number, index: number): boolean => { | ||
// ๋จ์ด๋ฅผ ๋ชจ๋ ์ฐพ์์ ๊ฒฝ์ฐ | ||
if (index === word.length) return true; | ||
|
||
// ๋ฒ์๋ฅผ ๋ฒ์ด๋๊ฑฐ๋ ๋ฌธ์๊ฐ ์ผ์นํ์ง ์๋ ๊ฒฝ์ฐ | ||
if (x < 0 || y < 0 || x >= cols || y >= rows || board[y][x] !== word[index]) { | ||
return false; | ||
} | ||
|
||
// ํ์ฌ ์์น ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ (์์ ์์ ) | ||
const temp = board[y][x]; | ||
board[y][x] = "#"; | ||
|
||
// ์ํ์ข์ฐ ํ์ | ||
for (const [dx, dy] of directions) { | ||
if (dfs(x + dx, y + dy, index + 1)) { | ||
return true; | ||
} | ||
} | ||
|
||
// ๋ฐฑํธ๋ํน: ์ ๊ฐ ๋ณต๊ตฌ | ||
board[y][x] = temp; | ||
|
||
return false; | ||
}; | ||
|
||
// board์์ word ์ฒซ๊ธ์๊ฐ ์ผ์นํ๋ ๊ฒฝ์ฐ ํ์ ์์ | ||
for (let y = 0; y < rows; y++) { | ||
for (let x = 0; x < cols; x++) { | ||
if (board[y][x] === word[0] && dfs(x, y, 0)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|