Skip to content

Commit

Permalink
Merge pull request #383 from tolluset/main
Browse files Browse the repository at this point in the history
[이병현] Week 3
  • Loading branch information
tolluset authored Aug 29, 2024
2 parents a2b3164 + 43f4a6d commit 513430b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
16 changes: 16 additions & 0 deletions climbing-stairs/tolluset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* TC: O(n)
* SC: O(n)
* */
function climbStairs(n: number): number {
const dp = new Array(n + 1).fill(0);

dp[1] = 1;
dp[2] = 2;

for (let i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}

return dp[n];
}
22 changes: 22 additions & 0 deletions coin-change/tolluset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* TC: O(amount * coins.length)
* SC: O(amount)
* */
function coinChange(coins: number[], amount: number): number {
if (amount === 0) {
return 0;
}

const dp = new Array(amount + 1).fill(Infinity);
dp[0] = 0;

for (let i = 1; i <= amount; i++) {
coins.forEach((coin) => {
if (coin <= i) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
});
}

return dp[amount] === Infinity ? -1 : dp[amount];
}
24 changes: 24 additions & 0 deletions combination-sum/tolluset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* TC: O(candidates.length ^ target / min(candidates))
* SC: O(target / min(candidates)
*/
function combinationSum(candidates: number[], target: number): number[][] {
const result: number[][] = [];
const dfs = (start: number, stop: number, path: number[]) => {
if (stop === 0) {
result.push([...path]);
return;
}

for (let i = start; i < candidates.length; i++) {
if (candidates[i] <= stop) {
path.push(candidates[i]);
dfs(i, stop - candidates[i], path);
path.pop();
}
}
};
dfs(0, target, []);

return result;
}
23 changes: 23 additions & 0 deletions product-of-array-except-self/tolluset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* TC: O(n)
* SC: O(n)
* */

function productExceptSelf(nums: number[]): number[] {
const n = nums.length;
const answer = new Array(n).fill(1);

let left = 1;
nums.forEach((num, i) => {
answer[i] = left;
left *= num;
});

let right = 1;
nums.reverse().forEach((num, i) => {
answer[n - 1 - i] *= right;
right *= num;
});

return answer;
}
21 changes: 21 additions & 0 deletions two-sum/tolluset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* TC: O(n)
* SC: O(n)
* */
function twoSum(nums: number[], target: number): number[] {
const n = nums.length;
const answer = new Map<number, number>();

for (let i = 0; i < n; i++) {
const diff = target - nums[i];
const before = answer.get(diff);

if (before) {
return [before, i];
}

answer.set(nums[i], i);
}

return [];
}

0 comments on commit 513430b

Please sign in to comment.