Skip to content

Commit

Permalink
Merge pull request #392 from heozeop/main
Browse files Browse the repository at this point in the history
[crispy] 3 week solution
  • Loading branch information
heozeop authored Aug 29, 2024
2 parents cfcdd05 + 274e467 commit 4480634
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
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];
}

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

0 comments on commit 4480634

Please sign in to comment.