-
-
Notifications
You must be signed in to change notification settings - Fork 245
[이병현] Week 3 #383
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
[이병현] Week 3 #383
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
818e279
solve: two sum
tolluset 3bfe29b
solve: climbing stairs
tolluset d9fd156
solve: product of array except self
tolluset aba1f66
solve: coin change
tolluset c60f537
solve: combination sum
tolluset 43f4a6d
chore: rename termination flag variable
tolluset 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,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]; | ||
} |
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,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]; | ||
} |
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,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, spot: number, path: number[]) => { | ||
if (spot === 0) { | ||
result.push([...path]); | ||
return; | ||
} | ||
|
||
for (let i = start; i < candidates.length; i++) { | ||
if (candidates[i] <= spot) { | ||
path.push(candidates[i]); | ||
dfs(i, spot - 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,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; | ||
} |
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,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 []; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.