Skip to content

Commit

Permalink
Merge pull request #402 from whewchews/main
Browse files Browse the repository at this point in the history
[pepper] Week 03 Solutions
  • Loading branch information
whewchews authored Aug 31, 2024
2 parents fa8a2f3 + f16e54a commit 71419dd
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 0 deletions.
56 changes: 56 additions & 0 deletions climbing-stairs/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 아이디어
* 측수 μ œν•œ: 1 <= n <= 45
* 1 or 2 step 만 올라갈 수 있음
* 1 -> [1]
* 2 -> [1,1] [2]
* 3 -> [1,1,1] [2,1] [1,2]
* 4 -> [1,1,1,1] [2,1,1] [1,2,1] [1,1,2] [2,2]
* 5 -> [1,1,1,1,1] [2,1,1,1] [1,2,1,1] [1,1,2,1] [1,1,1,2] [2,2,1], [1,2,2], [2,1,2]
* 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}
// for(let i = 3; i<=n; i++){
// stair[i] = stair[i-1] + stair[i-2]
// }
// TC: O(N)
// SC: O(N)

// # Solution 2

// if(n < 3) return n
// let curr = 2 // ν˜„μž¬ 계단을 였λ₯΄λŠ” 방법 수
// let prev = 1 // 이전 계단을 였λ₯΄λŠ” 방법 수

// for(let i=0; i<n-2; i++){
// const next = prev + curr;
// prev = curr;
// curr = next;
// }

// return curr
// TC: O(N)
// SC: O(1)

// # Solution 3: μž¬κ·€
const memo = { 1: 1, 2: 2 };
function calculateClimbingWay(n, 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);
// TC: O(N)
// SC: O(N)
}
33 changes: 33 additions & 0 deletions coin-change/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function coinChange(coins: number[], amount: number): number {
/* # Solution 1: BFS
* μ΅œμ†Œ 경둜λ₯Ό μ°ΎλŠ” 문제 => BFS
* ν˜„μž¬κΉŒμ§€ μ‚¬μš©ν•œ λ™μ „μ˜ κ°œμˆ˜μ™€ ν˜„μž¬κΉŒμ§€ μ‚¬μš©ν•œ λ™μ „μ˜ 합을 queue에 λ„£λŠ”λ‹€.
* visited: 쀑볡 방문을 λ°©μ§€ν•˜κΈ° μœ„ν•œ set
* λˆ„μ μ•‘μ΄ amount와 같아지면 countλ₯Ό return
* visited에 λˆ„μ μ•‘μ΄ 있으면 continue
* coinsλ₯Ό μˆœνšŒν•˜λ©΄μ„œ λˆ„μ μ•‘μ— 동전을 λ”ν•œ 값이 amount보닀 μž‘μœΌλ©΄ queue에 λ„£λŠ”λ‹€.
* queueκ°€ λΉŒλ•ŒκΉŒμ§€ 반볡
* 큐가 λΉ„μ–΄μžˆκ³  amountλ₯Ό λ§Œλ“€μˆ˜ μ—†μœΌλ©΄ -1을 return
*/
const queue = [[0, 0]]; // [number of coins, accumulated amount]
const visited = new Set();

while (queue.length > 0) {
const [count, total] = queue.shift();
if (total === amount) {
return count;
}
if (visited.has(total)) {
continue;
}
visited.add(total);
for (const coin of coins) {
if (total + coin <= amount) {
queue.push([count + 1, total + coin]);
}
}
}
return -1;
}
// TC: 각 κΈˆμ•‘(amount)λ§ˆλ‹€ 동전(coins)을 μˆœνšŒν•˜λ―€λ‘œ O(N^2*M) N: amount, M: coins.length
// SC: O(N) N: amount
118 changes: 118 additions & 0 deletions combination-sum/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
function combinationSum(candidates: number[], target: number): number[][] {
/*
* 합이 target이 λ˜λŠ” candidates 경우의 수λ₯Ό 리턴
* cadidatesλŠ” κ³ μœ ν•¨
* 합이 target이 λ˜μ§€ μ•ŠλŠ” 경우, λΉˆλ°°μ—΄ 리턴
* cadidate을 쀑볡 μ‚¬μš© κ°€λŠ₯
* 2 <= candidates[i] <= 40
* => candidate이 1인 κ²½μš°λŠ” μ—†μŒ
* candidatesμ—μ„œ target보닀 κ°™κ±°λ‚˜ μž‘μ€ κ°’λ§Œ filterν•˜κ³  μ‹œμž‘ (target보닀 큰 값은 후보ꡰ이 될 수 μ—†μŒ)
*
* [2,3,6,7] / 7
*
* []
* [2] / 5
* [2, 2] 3 => X
* [2, 2, 2] 1 => X
* [2, 2, 2, 2] -1 => X
* [2, 2, 2, 3] -2 => X
* [2, 2, 2, 6] -5 => X
* [2, 2, 2, 7] -6 => X
[2, 2, 3] 0 => O
// ...
[2, 3] 2 => X
[2, 3, 2] 0 => O
// ...
[2, 6] -1 => X
// ...
*
* ν•˜λ‚˜μ”© 값을 μΆ”κ°€ν•˜λ©΄μ„œ λ°°μ—΄μ˜ 총합을 target κ°’κ³Ό λΉ„κ΅ν•œλ‹€
* sum이 target값보닀 μž‘μœΌλ©΄ 계속 λ‹€μŒ 값을 μΆ”κ°€ν•΄μ€€λ‹€
* sum이 targetκ³Ό κ°™μœΌλ©΄ κ²°κ³Ό κ°’ result 배열에 μΆ”κ°€ν•΄μ€€λ‹€.
* sum이 target보닀 λ„˜μœΌλ©΄ λ§ˆμ§€λ§‰μ— μΆ”κ°€ν•œ 값을 λΊ€λ‹€.
* 이 과정을 λ°˜λ³΅ν•˜λ©° λ°°μ—΄μ—μ„œ κ²°κ³Ό 값을 μ°ΎλŠ”λ‹€.
*
*/


function backtrack(candidates: number[], start:number, total:number){
if(target === total){
result.push([...path])
return
}

if(target < total){
return
}

for(let i=start; i<=candidates.length-1; i++){
path.push(candidates[i])
backtrack(candidates, i,total + candidates[i])
path.pop()
}
}

const result = []
const path = []
// TC: O(NlogN)
// SC: O(N)
const filteredCandidates = candidates.filter(candidate => candidate<=target).sort((a,b)=> a-b)
backtrack(filteredCandidates, 0, 0)
return result

};
// TC: O(n^t) n = candidates.length, t = target 크기
// SC: O(t)

/* #Solution 2 : DP
* candidates을 가지고 target 값을 λ§Œλ“€ 수 μžˆλŠ” λͺ¨λ“  쑰합을 미리 μ°Ύμ•„λ‘”λ‹€.
*candidates [2,3,6,7] / target 7 라고 ν–ˆμ„λ•Œ
* 1) candidate = 2
* dp[2] = [[2]]
* dp[4] = [[2,2]]
* dp[6] = [[2,2,2]]
* 2) candidate = 3
* dp[3] = [[3]]
* dp[5] = [[2,3]]
* dp[6] = [[2,2,2], [3,3]]
* dp[7] = [[2,2,3]]
* 3) candidate = 6
* dp[6] = [[[2,2,2], [3,3], [6]]
* 4) candidate = 7
* dp[7] = [[2,2,3], [7]]
*
* => dp = [
* [ [] ]
* [ [] ]
* [ [2] ]
* [[3]]
* [[2,2]]
* [[2,3,]]
* [[2,2,2], [3,3], [6]]
* [[2,2,3], [7]]
* ]
* ]
* /



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


for (let candidate of candidates) {
for (let i = candidate; i <= target; i++) {
for (let combination of dp[i - candidate]) {
dp[i].push([...combination, candidate]);
}
}
}

return dp[target];
}

// TC: O(n * t * 2^n) n = candidates.length, t = target
// SC: O((t*2^n) // μ΅œμ•…μ˜ 경우 λͺ¨λ“  μ‘°ν•©(2^n) μ €μž₯ κ°€λŠ₯
70 changes: 70 additions & 0 deletions product-of-array-except-self/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* #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[] {
// let zeroCount = 0;
// const sum = nums.reduce((p, c) => {
// if (c === 0) {
// zeroCount += 1;
// return p;
// }
// p = p * c;
// return p;
// }, 1);

// const hasZero = zeroCount > 0;

// if (zeroCount === nums.length) return Array(nums.length).fill(0);

// return nums.map((n) => {
// if (n === 0) {
// // μžμ‹  이외에 0이 μžˆμ„λ•Œ
// if (zeroCount - 1 > 0) {
// return 0;
// }

// return sum;
// }

// if (hasZero) return 0;
// return sum / n;
// });
// TC: O(N)
// SC: O(N)

/* #Solution 2
* 1. prefix: μžμ‹ μ„ μ œμ™Έν•œ 자기 인덱슀 μ•žκΉŒμ§€μ˜ 곱을 μ €μž₯ν•œλ‹€.
* 2. suffix: μžμ‹ μ„ μ œμ™Έν•œ 자기 λ’€κΉŒμ§€μ˜ 곱을 μ €μž₯ν•œλ‹€.
* 3. answer[i] = prefix[i] * suffix[i]
*/
const n = nums.length;

const prefix = new Array(n).fill(1);
const suffix = new Array(n).fill(1);

for (let i = 1; i < n; i++) {
prefix[i] = prefix[i - 1] * nums[i - 1];
}

for (let i = n - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1] * nums[i + 1];
}

const answer = [];
for (let i = 0; i < n; i++) {
answer[i] = prefix[i] * suffix[i];
}

return answer;
}
// TC: O(N)
// SC: O(N)
27 changes: 27 additions & 0 deletions two-sum/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 아이디어
* μ²˜μŒλΆ€ν„° 배열을 μˆœνšŒν•˜λ©° [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[] {
// SC: O(N)
const dict = new Map();

// TC: O(N)
for (let i = 0; i <= nums.length - 1; i++) {
const curr = nums[i];
const pairValue = target - curr;
if (dict.has(pairValue)) {
return [dict.get(pairValue), i];
}

dict.set(curr, i);
}
}

// TC: O(N)
// SC: O(N)

0 comments on commit 71419dd

Please sign in to comment.