-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #385 from obzva/main
[Flynn] week 3
- Loading branch information
Showing
5 changed files
with
117 additions
and
0 deletions.
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; | ||
} | ||
}; |