Skip to content
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 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이전 풀이도 그렇고, 큐와 스택을 정말 잘 쓰시네요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다
재귀보다는 반복문 사용하는 풀이를 좀 더 선호합니다 :D

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;
}
};