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

[crispy] 3 week solution #392

Merged
merged 9 commits into from
Aug 29, 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
20 changes: 20 additions & 0 deletions climbing-stairs/heozeop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Time Complexity: O(n)
// Spatial Complexity: O(n)

class Solution {
public:
int climbStairs(int n) {
vector<int> dp(n + 1, 0);

if (n == 1) {
return 1;
}

dp[0] = dp[1] = 1;
for(int i = 2; i <= n; ++i) {
dp[i] = dp[i - 1] + dp[i - 2];
Copy link
Contributor

Choose a reason for hiding this comment

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

명확한 점화식이네요!

}

return dp[n];
}
};
33 changes: 33 additions & 0 deletions coin-change/heozeop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity: O(n * amount)
// Spatial Complexity: O(amount)

const int MAX_VALUE = 10001;
const int IMPOSSIBLE = -1;

class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
Copy link
Contributor

Choose a reason for hiding this comment

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

dp로 가능하군요..?!
저는 dp로 풀려다가 점화식이 도저히 생각 나지 않아서 bfs로 구현했는데 점화식을 작성하기까지 어떻게 생각하셨나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

일단 bfs로 먼저 풀고, 점화식을 고민했어요!
결국 최소 개수만 있으면 된다는 점에서 dp로 가능하겠다고 생각했어요!

if (amount == 0) {
return 0;
}

vector<int> dp(amount + 1, MAX_VALUE);

dp[0] = 0;
for(int i = 0; i <= amount; ++i) {
for(int coin : coins) {
if (i < coin) {
continue;
}

dp[i] = min(1 + dp[i - coin], dp[i]);
}
}

if (dp[amount] == MAX_VALUE) {
return IMPOSSIBLE;
}

return dp[amount];
}
};
37 changes: 37 additions & 0 deletions combination-sum/heozeop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Time Complexity: O(n^target);
// - target에 비례하는 tree 깊이에 따라 n번 순회 발생
// Spatial Complexity: O(target);
// - target에 비례하는 visited vector, answer vector만 있으면 됨.

class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> visited;
backtrack(ans, candidates, visited, target, 0);
return ans;
}

void backtrack(
vector<vector<int>>& ans,
vector<int>& candidates,
vector<int>& visited,
int target,
int prev
) {
if(target == 0) {
ans.push_back(visited);
return;
}

for(int i = prev; i < candidates.size(); ++i) {
if (target - candidates[i] < 0) {
continue;
}

visited.push_back(candidates[i]);
backtrack(ans, candidates, visited, target - candidates[i], i);
visited.pop_back();
}
}
};
43 changes: 43 additions & 0 deletions product-of-array-except-self/heozeop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Time complexity: O(n)
// Spatial complexity: O(n)

class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int numberOfZero = 0, productNums = 1;

for (int num : nums) {
if(num == 0) {
++numberOfZero;
continue;
}

productNums *= num;
}

vector<int> answer(nums.size(), 0);
if (numberOfZero > 1) {
return answer;
}

if (numberOfZero == 1) {
for(int i = 0; i < nums.size(); ++i) {
if(nums[i] == 0) {
answer[i] = productNums;
return answer;
}
}
}

for(int i = 0; i < nums.size(); ++i) {
if (nums[i] == 0) {
answer[i] = productNums;
continue;
}

answer[i] = productNums / nums[i];
}

return answer;
}
};
35 changes: 35 additions & 0 deletions two-sum/heozeop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Time Complexity: O(nlogn)
// Spatial Complexity: O(n)

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
Copy link
Contributor

Choose a reason for hiding this comment

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

투포인터를 사용하셨군요! 시간복잡도를 O(n) 으로 줄일 수 있는 방법도 있는데 나중에 시도해보시면 어떨까요?

bool found = false;
vector<pair<int,int>> temp(nums.size());

for(int i = 0; i < nums.size(); ++i) {
temp[i] = make_pair(nums[i], i);
}

sort(temp.begin(), temp.end());

int start = 0, end = temp.size() - 1, sum;
while(start < end) {
sum = temp[start].first + temp[end].first;

if (sum == target) {
break;
}

if (sum > target) {
--end;
}

if (sum < target) {
++start;
}
}

return vector<int>({temp[start].second, temp[end].second});
}
};