-
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
[crispy] 3 week solution #392
Changes from all commits
a63a19b
473db1e
8ed3379
26f7ac4
831cfa4
755ef07
1ba8c56
0e55211
274e467
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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]; | ||
} | ||
|
||
return dp[n]; | ||
} | ||
}; |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dp로 가능하군요..?! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 일단 bfs로 먼저 풀고, 점화식을 고민했어요! |
||
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]; | ||
} | ||
}; |
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(); | ||
} | ||
} | ||
}; |
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; | ||
} | ||
}; |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 투포인터를 사용하셨군요! 시간복잡도를 |
||
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}); | ||
} | ||
}; |
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.
명확한 점화식이네요!