diff --git a/climbing-stairs/heozeop.cpp b/climbing-stairs/heozeop.cpp new file mode 100644 index 000000000..b255bb1a4 --- /dev/null +++ b/climbing-stairs/heozeop.cpp @@ -0,0 +1,20 @@ +// Time Complexity: O(n) +// Spatial Complexity: O(n) + +class Solution { +public: + int climbStairs(int n) { + vector 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]; + } +}; diff --git a/coin-change/heozeop.cpp b/coin-change/heozeop.cpp new file mode 100644 index 000000000..6a173d6e5 --- /dev/null +++ b/coin-change/heozeop.cpp @@ -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& coins, int amount) { + if (amount == 0) { + return 0; + } + + vector 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]; + } +}; diff --git a/combination-sum/heozeop.cpp b/combination-sum/heozeop.cpp new file mode 100644 index 000000000..27555c75b --- /dev/null +++ b/combination-sum/heozeop.cpp @@ -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> combinationSum(vector& candidates, int target) { + vector> ans; + vector visited; + backtrack(ans, candidates, visited, target, 0); + return ans; + } + + void backtrack( + vector>& ans, + vector& candidates, + vector& 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(); + } + } +}; diff --git a/product-of-array-except-self/heozeop.cpp b/product-of-array-except-self/heozeop.cpp new file mode 100644 index 000000000..c0e970f53 --- /dev/null +++ b/product-of-array-except-self/heozeop.cpp @@ -0,0 +1,43 @@ +// Time complexity: O(n) +// Spatial complexity: O(n) + +class Solution { +public: + vector productExceptSelf(vector& nums) { + int numberOfZero = 0, productNums = 1; + + for (int num : nums) { + if(num == 0) { + ++numberOfZero; + continue; + } + + productNums *= num; + } + + vector 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; + } +}; diff --git a/two-sum/heozeop.cpp b/two-sum/heozeop.cpp new file mode 100644 index 000000000..696fdfe67 --- /dev/null +++ b/two-sum/heozeop.cpp @@ -0,0 +1,35 @@ +// Time Complexity: O(nlogn) +// Spatial Complexity: O(n) + +class Solution { +public: + vector twoSum(vector& nums, int target) { + bool found = false; + vector> 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({temp[start].second, temp[end].second}); + } +};