-
Notifications
You must be signed in to change notification settings - Fork 126
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
[Flynn] week 3 #385
Merged
Merged
[Flynn] week 3 #385
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2695f42
Solution: Two Sum
obzva fdd4d40
Solution: Climbing Stairs
obzva c8db868
Solution: Product of Array Except Self
obzva 7491493
Solution: Combination Sum
obzva f6d75c6
Solution: Coin Change
obzva c6de304
Fix: md -> cpp
obzva 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 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 @@ | ||
class Solution { | ||
public: | ||
int climbStairs(int n) { | ||
vector<int> memo(2, 1); | ||
|
||
for (int i = 2; i <= n; i++) { | ||
memo.push_back(memo[i - 1] + memo[i - 2]); | ||
} | ||
|
||
return memo[n]; | ||
} | ||
|
||
}; |
This file contains 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 @@ | ||
class Solution { | ||
public: | ||
int coinChange(vector<int>& coins, int amount) { | ||
int MAX = 10000 + 1; | ||
vector<int> memo(amount + 1, MAX); | ||
memo[0] = 0; | ||
|
||
for (int i = 1; i <= amount; i++) { | ||
for (auto coin : coins) { | ||
if (i - coin >= 0) { | ||
memo[i] = min(memo[i], memo[i - coin] + 1); | ||
} | ||
} | ||
} | ||
|
||
return memo[amount] == MAX ? -1 : memo[amount]; | ||
} | ||
|
||
}; |
This file contains 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,43 @@ | ||
class Solution { | ||
public: | ||
vector<vector<int>> combinationSum(vector<int>& candidates, int target) { | ||
vector<vector<int>> res; | ||
queue<pair<int, pair<int, vector<int>>>> q; // {acc, {idx, combination}} | ||
|
||
for (int i = 0; i < candidates.size(); i++) { | ||
int num = candidates[i]; | ||
|
||
if (num <= target) { | ||
vector<int> comb; | ||
comb.push_back(num); | ||
q.push({num, {i, comb}}); | ||
} | ||
|
||
} | ||
|
||
while (!q.empty()) { | ||
auto p = q.front(); | ||
q.pop(); | ||
|
||
int acc = p.first, idx = p.second.first; | ||
auto comb = p.second.second; | ||
|
||
if (acc == target) { | ||
res.push_back(comb); | ||
} else if (acc < target) { | ||
for (int i = idx; i < candidates.size(); i++) { | ||
int num = candidates[i]; | ||
|
||
if (acc + num <= target) { | ||
vector<int> new_comb(comb); | ||
new_comb.push_back(num); | ||
q.push({acc + num, {i, new_comb}}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
|
||
}; |
This file contains 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 @@ | ||
class Solution { | ||
public: | ||
vector<int> productExceptSelf(vector<int>& nums) { | ||
vector<int> res(nums.size(), 1); | ||
|
||
for (int i = 1; i < nums.size(); i++) { | ||
res[i] *= nums[i - 1] * res[i - 1]; | ||
} | ||
|
||
int acc = 1; | ||
for (int i = nums.size() - 2; i >= 0; i--) { | ||
acc *= nums[i + 1]; | ||
res[i] *= acc; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
}; |
This file contains 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 @@ | ||
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
unordered_map<int, int> past; // key: num value: index | ||
vector<int> res; | ||
|
||
past.insert({nums[0], 0}); | ||
|
||
for (int i = 1; i < nums.size(); i++) { | ||
int remainder = target - nums[i]; | ||
|
||
if (past.find(remainder) != past.end()) { | ||
res.push_back(i); | ||
res.push_back(past[remainder]); | ||
break; | ||
} else { | ||
past.insert({nums[i], i}); | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
}; |
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.
이전 풀이도 그렇고, 큐와 스택을 정말 잘 쓰시네요
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.
감사합니다
재귀보다는 반복문 사용하는 풀이를 좀 더 선호합니다 :D