Skip to content

Commit

Permalink
Merge pull request #829 from Yjason-K/main
Browse files Browse the repository at this point in the history
[gomgom22] Week4
  • Loading branch information
Yjason-K authored Jan 4, 2025
2 parents 5b454a8 + 07fa579 commit e97a84e
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 0 deletions.
46 changes: 46 additions & 0 deletions coin-change/Yjason-K.ts
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;
}

49 changes: 49 additions & 0 deletions merge-two-sorted-lists/Yjason-K.ts
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;
}
29 changes: 29 additions & 0 deletions missing-number/Yjason-K.ts
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;
}
48 changes: 48 additions & 0 deletions palindromic-substrings/Yjason-K.ts
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;
}
70 changes: 70 additions & 0 deletions word-search/Yjason-K.ts
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;
}

0 comments on commit e97a84e

Please sign in to comment.