Skip to content

Commit

Permalink
fix: TC, SC
Browse files Browse the repository at this point in the history
  • Loading branch information
whewchews committed Aug 31, 2024
1 parent b7e3dd7 commit f16e54a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 44 deletions.
18 changes: 9 additions & 9 deletions climbing-stairs/whewchews.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function climbStairs(n: number): number {
/*
/*
* ์•„์ด๋””์–ด
* ์ธต์ˆ˜ ์ œํ•œ: 1 <= n <= 45
* 1 or 2 step ๋งŒ ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ์Œ
Expand All @@ -12,7 +11,7 @@ function climbStairs(n: number): number {
* 6 -> [1,1,1,1,1,1] [2,1,1,1,1] [...] [1,1,1,1,2] [2,2,1,1], [2,1,2,1], [2,1,1,2] [1,1,2,2], [1,2,1,2], [1,2,2,1]
=> (1:n, 2:0) n๊ฐ€์ง€ (1:n-2, 2:1) / n๊ฐ€์ง€ (1: n-4, 2: n/2) C(n, n/2) ๊ฐ€์ง€
*/

function climbStairs(n: number): number {
// # Solution 1

// const stair = {1: 1, 2:2}
Expand Down Expand Up @@ -41,13 +40,14 @@ function climbStairs(n: number): number {
// # Solution 3: ์žฌ๊ท€
const memo = { 1: 1, 2: 2 };
function calculateClimbingWay(n, memo) {
if (!(n in memo)) {
if (n < 3) {
return n;
}
memo[n] =
calculateClimbingWay(n - 1, memo) + calculateClimbingWay(n - 2, memo);
if (n in memo) return memo[n];

if (n < 3) {
return n;
}
memo[n] =
calculateClimbingWay(n - 1, memo) + calculateClimbingWay(n - 2, memo);

return memo[n];
}
return calculateClimbingWay(n, memo);
Expand Down
5 changes: 3 additions & 2 deletions coin-change/whewchews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ function coinChange(coins: number[], amount: number): number {
* coins๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋ˆ„์ ์•ก์— ๋™์ „์„ ๋”ํ•œ ๊ฐ’์ด amount๋ณด๋‹ค ์ž‘์œผ๋ฉด queue์— ๋„ฃ๋Š”๋‹ค.
* queue๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
* ํ๊ฐ€ ๋น„์–ด์žˆ๊ณ  amount๋ฅผ ๋งŒ๋“ค์ˆ˜ ์—†์œผ๋ฉด -1์„ return
*/ const queue = [[0, 0]]; // [number of coins, accumulated amount]
*/
const queue = [[0, 0]]; // [number of coins, accumulated amount]
const visited = new Set();

while (queue.length > 0) {
Expand All @@ -28,5 +29,5 @@ function coinChange(coins: number[], amount: number): number {
}
return -1;
}
// TC: ๊ฐ ๊ธˆ์•ก(amount)๋งˆ๋‹ค ๋™์ „(coins)์„ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(N*M) N: amount, M: coins.length
// TC: ๊ฐ ๊ธˆ์•ก(amount)๋งˆ๋‹ค ๋™์ „(coins)์„ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(N^2*M) N: amount, M: coins.length
// SC: O(N) N: amount
17 changes: 6 additions & 11 deletions combination-sum/whewchews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ function combinationSum(candidates: number[], target: number): number[][] {
*
*/

// TC: O(2^N) N = candidates.length
// SC: O(T) T = target

function backtrack(candidates: number[], start:number, total:number){
if(target === total){
result.push([...path])
Expand All @@ -64,8 +63,8 @@ function combinationSum(candidates: number[], target: number): number[][] {
return result

};
// TC: O(2^N)
// SC: O(2^N)
// TC: O(n^t) n = candidates.length, t = target ํฌ๊ธฐ
// SC: O(t)

/* #Solution 2 : DP
* candidates์„ ๊ฐ€์ง€๊ณ  target ๊ฐ’์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์กฐํ•ฉ์„ ๋ฏธ๋ฆฌ ์ฐพ์•„๋‘”๋‹ค.
Expand Down Expand Up @@ -99,17 +98,13 @@ function combinationSum(candidates: number[], target: number): number[][] {



// SC: O(T+1) T = target
// SC: O(t) t = target
const dp = Array.from({ length: target + 1 }, () => []);
dp[0] = [[]];


// TC: O(C), C = candidates.length
for (let candidate of candidates) {
// TC: O(T) T = target
for (let i = candidate; i <= target; i++) {
// TC: O(K) K = dp[i - candidate].length
// SC: O(K)
for (let combination of dp[i - candidate]) {
dp[i].push([...combination, candidate]);
}
Expand All @@ -119,5 +114,5 @@ function combinationSum(candidates: number[], target: number): number[][] {
return dp[target];
}

// TC: O(C * T * K)
// SC: O((T+1) * K* T)
// TC: O(n * t * 2^n) n = candidates.length, t = target
// SC: O((t*2^n) // ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ์กฐํ•ฉ(2^n) ์ €์žฅ ๊ฐ€๋Šฅ
24 changes: 12 additions & 12 deletions product-of-array-except-self/whewchews.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* #Solution 1
* sum: ์ „์ฒด ๊ณฑ์„ ๊ตฌํ•œ๋‹ค.
* zeroCount: 0์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ช‡๊ฐœ์ธ์ง€ ์„ผ๋‹ค.
* 1. ์ž์‹ ์ด 0์ด๋ฉด,
* 1-1. ์ž์‹ ์ด์™ธ์˜ 0์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์žˆ์œผ๋ฉด 0์„ return
* 1-2. ์ž์‹  ์ด์™ธ์— 0์ด ์—†์œผ๋ฉด ์ „์ฒด ๊ณฑ์„ return
* 2. ์ž์‹ ์ด 0์ด ์•„๋‹ˆ๋ฉด
* 2-1. zeroCount๊ฐ€ ์žˆ๋Š”์ง€ ๋ณด๊ณ  ์žˆ์œผ๋ฉด 0์„ return
* 2-2. zero๊ฐ€ ์—†์œผ๋ฉด sum/self๋ฅผ return
*
* ๊ทธ๋Ÿฌ๋‚˜... ๋ฌธ์ œ์— ๋‚˜๋ˆ„๊ธฐ ์—ฐ์‚ฐ์ž๋ฅผ ์“ฐ๋ฉด ์•ˆ๋œ๋‹ค๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ Solution 2๋กœ ๊ฐ€์ž.
*/
function productExceptSelf(nums: number[]): number[] {
/* #Solution 1
* sum: ์ „์ฒด ๊ณฑ์„ ๊ตฌํ•œ๋‹ค.
* zeroCount: 0์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ช‡๊ฐœ์ธ์ง€ ์„ผ๋‹ค.
* 1. ์ž์‹ ์ด 0์ด๋ฉด,
* 1-1. ์ž์‹ ์ด์™ธ์˜ 0์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์žˆ์œผ๋ฉด 0์„ return
* 1-2. ์ž์‹  ์ด์™ธ์— 0์ด ์—†์œผ๋ฉด ์ „์ฒด ๊ณฑ์„ return
* 2. ์ž์‹ ์ด 0์ด ์•„๋‹ˆ๋ฉด
* 2-1. zeroCount๊ฐ€ ์žˆ๋Š”์ง€ ๋ณด๊ณ  ์žˆ์œผ๋ฉด 0์„ return
* 2-2. zero๊ฐ€ ์—†์œผ๋ฉด sum/self๋ฅผ return
*
* ๊ทธ๋Ÿฌ๋‚˜... ๋ฌธ์ œ์— ๋‚˜๋ˆ„๊ธฐ ์—ฐ์‚ฐ์ž๋ฅผ ์“ฐ๋ฉด ์•ˆ๋œ๋‹ค๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ Solution 2๋กœ ๊ฐ€์ž.
*/
// let zeroCount = 0;
// const sum = nums.reduce((p, c) => {
// if (c === 0) {
Expand Down
19 changes: 9 additions & 10 deletions two-sum/whewchews.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/*
* ์•„์ด๋””์–ด
* ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ [0,1] [0,2] [0,3] [0, ...], [1,2], [1,3], [1,...] ... [nums.length-2, nums.length-1] ๋ฅผ ๋Œ๋ฉด์„œ ๋‘ ์ธ์ž์˜ ํ•ฉ์ด target์ด ๋˜๋Š” ์ง€์ ์„ ์ฐพ๋Š”๋‹ค.
* ์ˆœ์„œ๋Œ€๋กœ ๋Œ๋ฉด ์ตœ์•…์˜ ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ๊นŒ์ง€ ๊ฐˆ ์ˆ˜๋„ ์žˆ๋‹ค.
* ์ ˆ๋Œ€ ๊ฐ’์ด ๋˜์ง€ ๋ชปํ•˜๋Š”, ์ตœ์†Œ ์กฐ๊ฑด์„ ์ƒ๊ฐํ•ด๋ณด์ž.
* ์ฃผ์˜1:๋ฒ”์œ„๊ฐ€ -10^9 <= nums[i] <= 10^9๋กœ ์Œ์ˆ˜๊ฐ’๋„ ์žˆ์Œ
* ์ฃผ์˜2: ์ •๋ ฌ๋œ ์ˆœ์„œ๊ฐ€ ์•„๋‹˜.
* ๊ฐ’์„ Map์— ์ €์žฅํ•ด๋‘๊ณ  {value: index}, Map์— ์ž์‹ ๊ณผ ๋”ํ–ˆ์„๋•Œ target์ด ๋‚˜์˜ค๋Š” value๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
*/
function twoSum(nums: number[], target: number): number[] {
/*
* ์•„์ด๋””์–ด
* ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ [0,1] [0,2] [0,3] [0, ...], [1,2], [1,3], [1,...] ... [nums.length-2, nums.length-1] ๋ฅผ ๋Œ๋ฉด์„œ ๋‘ ์ธ์ž์˜ ํ•ฉ์ด target์ด ๋˜๋Š” ์ง€์ ์„ ์ฐพ๋Š”๋‹ค.
* ์ˆœ์„œ๋Œ€๋กœ ๋Œ๋ฉด ์ตœ์•…์˜ ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ๊นŒ์ง€ ๊ฐˆ ์ˆ˜๋„ ์žˆ๋‹ค.
* ์ ˆ๋Œ€ ๊ฐ’์ด ๋˜์ง€ ๋ชปํ•˜๋Š”, ์ตœ์†Œ ์กฐ๊ฑด์„ ์ƒ๊ฐํ•ด๋ณด์ž.
* ์ฃผ์˜1:๋ฒ”์œ„๊ฐ€ -10^9 <= nums[i] <= 10^9๋กœ ์Œ์ˆ˜๊ฐ’๋„ ์žˆ์Œ
* ์ฃผ์˜2: ์ •๋ ฌ๋œ ์ˆœ์„œ๊ฐ€ ์•„๋‹˜.
* ๊ฐ’์„ Map์— ์ €์žฅํ•ด๋‘๊ณ  {value: index}, Map์— ์ž์‹ ๊ณผ ๋”ํ–ˆ์„๋•Œ target์ด ๋‚˜์˜ค๋Š” value๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
*/

// SC: O(N)
const dict = new Map();

Expand Down

0 comments on commit f16e54a

Please sign in to comment.