Skip to content

[강희찬] WEEK 3 Solution #384

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

Merged
merged 8 commits into from
Aug 28, 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
11 changes: 11 additions & 0 deletions climbing-stairs/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// T.C. O(n)
// S.C. O(1)
function climbStairs(n: number): number {
let p = 0;
let q = 1;
for (let i = 0; i < n; i++) {
q = q + p;
p = q - p;
}
return q;
}
15 changes: 15 additions & 0 deletions coin-change/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// T.C: O(coins.length * amount)
// S.C: O(amount)
function coinChange(coins: number[], amount: number): number {
if (amount == 0) return 0;
const dp = new Array(amount + 1).fill(Infinity);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 인피니티 쓰신거 신기하네영 👀

dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (let j = 0; j < coins.length; j++) {
if (coins[j] <= i) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
};
19 changes: 19 additions & 0 deletions combination-sum/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// T.C: O(n^t) // n: candidates.length, t: target
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주석에 어떤 항목인지 써주신거 좋네여 👍

// S.C: O(n)
function combinationSum(candidates: number[], target: number): number[][] {
const result: number[][] = [];
function dfs(start: number, target: number, path: number[]) {
if (target < 0) return;
if (target === 0) {
result.push([...path]);
return;
}
for (let i = start; i < candidates.length; i++) {
path.push(candidates[i]);
dfs(i, target - candidates[i], path);
path.pop();
}
}
dfs(0, target, []);
return result;
};
18 changes: 18 additions & 0 deletions product-of-array-except-self/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// T.C: O(n)
// S.C: O(n)
function productExceptSelf(nums: number[]): number[] {
const arr: number[] = new Array(nums.length);
let product = 1;
for (let i = 0; i < nums.length; i++) {
arr[i] = product;
product *= nums[i];
}

product = 1;
for (let i = nums.length - 1; i >= 0; i--) {
arr[i] *= product;
product *= nums[i];
}

return arr;
};
13 changes: 13 additions & 0 deletions two-sum/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// T.C. O(n)
// S.C. O(n)
function twoSum(nums: number[], target: number): number[] {
const sumMap = new Map<number, number>();
for (const [i, num] of nums.entries()) {
const diff = target - num;
if (sumMap.has(diff)) {
return [sumMap.get(diff)!, i];
}
sumMap.set(num, i);
}
return [];
}