Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[이병현] Week 3 #383

Merged
merged 6 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [];
}