Skip to content

Commit

Permalink
Merge pull request #385 from obzva/main
Browse files Browse the repository at this point in the history
[Flynn] week 3
  • Loading branch information
obzva authored Aug 28, 2024
2 parents e33cc14 + c6de304 commit 862141b
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
13 changes: 13 additions & 0 deletions climbing-stairs/flynn.cpp
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];
}

};
19 changes: 19 additions & 0 deletions coin-change/flynn.cpp
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];
}

};
43 changes: 43 additions & 0 deletions combination-sum/flynn.cpp
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;
}

};
19 changes: 19 additions & 0 deletions product-of-array-except-self/flynn.cpp
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;
}

};
23 changes: 23 additions & 0 deletions two-sum/flynn.cpp
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;
}
};

0 comments on commit 862141b

Please sign in to comment.