-
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 #392 from heozeop/main
[crispy] 3 week solution
- Loading branch information
Showing
5 changed files
with
168 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,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]; | ||
} | ||
}; |
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,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) { | ||
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]; | ||
} | ||
}; |
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,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(); | ||
} | ||
} | ||
}; |
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 @@ | ||
// 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; | ||
} | ||
}; |
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,35 @@ | ||
// Time Complexity: O(nlogn) | ||
// Spatial Complexity: O(n) | ||
|
||
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
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}); | ||
} | ||
}; |