-
-
Notifications
You must be signed in to change notification settings - Fork 195
[강희찬] 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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c557325
feat: 1. Two Sum
HC-kang 0c58a2c
feat: 70. Climbing Stairs
HC-kang 7e66c54
simplify: 70. Climbing Stairs
HC-kang f84dc87
feat: 238. Product of Array Except Self
HC-kang 414efbc
feat: 322. Coin Change
HC-kang 9c0d2b1
feat: 39. Combination Sum
HC-kang aa71427
docs: update 39. Combination Sum
HC-kang 64bbd50
docs: update 39. Combination Sum
HC-kang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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]; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// T.C: O(n^t) // n: candidates.length, t: target | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 인피니티 쓰신거 신기하네영 👀