From a63a19b0f3c0148a7bd5d2b7b5ca65e3fe443753 Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Tue, 27 Aug 2024 18:45:12 +0900 Subject: [PATCH 1/9] Create heozeop.cpp --- two-sum/heozeop.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 two-sum/heozeop.cpp diff --git a/two-sum/heozeop.cpp b/two-sum/heozeop.cpp new file mode 100644 index 000000000..00541c425 --- /dev/null +++ b/two-sum/heozeop.cpp @@ -0,0 +1,35 @@ +// Time Complexity: O(nlogn) +// Spatial Complexity: O(nlogn) + +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}); + } +}; From 473db1eab0f7ce873d9d687b0bc8487bd3eed314 Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Tue, 27 Aug 2024 18:51:16 +0900 Subject: [PATCH 2/9] Create heozeop.cpp --- climbing-stairs/heozeop.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 climbing-stairs/heozeop.cpp 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]; + } +}; From 8ed3379c59e0662c437642aee16fc7003c7470eb Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Tue, 27 Aug 2024 19:08:44 +0900 Subject: [PATCH 3/9] Create heozeop.cpp --- product-of-array-except-self/heozeop.cpp | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 product-of-array-except-self/heozeop.cpp 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; + } +}; From 26f7ac4149933c48b1286eee83f8fdaada908ae5 Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Tue, 27 Aug 2024 19:15:53 +0900 Subject: [PATCH 4/9] Create heozeop.cpp --- combination-sum/heozeop.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 combination-sum/heozeop.cpp diff --git a/combination-sum/heozeop.cpp b/combination-sum/heozeop.cpp new file mode 100644 index 000000000..f61691f69 --- /dev/null +++ b/combination-sum/heozeop.cpp @@ -0,0 +1,35 @@ +// Time Complexity: O(n) +// Spatial Complexity: O(n); + +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(); + } + } +}; From 831cfa44676e845f3ab68f7de78c810e5c346a68 Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Tue, 27 Aug 2024 19:19:15 +0900 Subject: [PATCH 5/9] Update heozeop.cpp --- combination-sum/heozeop.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/combination-sum/heozeop.cpp b/combination-sum/heozeop.cpp index f61691f69..e7624ac73 100644 --- a/combination-sum/heozeop.cpp +++ b/combination-sum/heozeop.cpp @@ -1,4 +1,4 @@ -// Time Complexity: O(n) +// Time Complexity: O(n^2) // Spatial Complexity: O(n); class Solution { From 755ef07bfe1b9ade5dca3f20dc4c99e4fc508467 Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Tue, 27 Aug 2024 20:13:53 +0900 Subject: [PATCH 6/9] Create heozeop.cpp --- coin-change/heozeop.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 coin-change/heozeop.cpp diff --git a/coin-change/heozeop.cpp b/coin-change/heozeop.cpp new file mode 100644 index 000000000..68b53a1ae --- /dev/null +++ b/coin-change/heozeop.cpp @@ -0,0 +1,37 @@ +// 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; + } + + if (dp[i - coin] == MAX_VALUE) { + continue; + } + + dp[i] = min(1 + dp[i - coin], dp[i]); + } + } + + if (dp[amount] == MAX_VALUE) { + return IMPOSSIBLE; + } + + return dp[amount]; + } +}; From 1ba8c565ac813e672ea0a498571368f35c042143 Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Tue, 27 Aug 2024 20:15:06 +0900 Subject: [PATCH 7/9] Update heozeop.cpp --- two-sum/heozeop.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/two-sum/heozeop.cpp b/two-sum/heozeop.cpp index 00541c425..696fdfe67 100644 --- a/two-sum/heozeop.cpp +++ b/two-sum/heozeop.cpp @@ -1,5 +1,5 @@ // Time Complexity: O(nlogn) -// Spatial Complexity: O(nlogn) +// Spatial Complexity: O(n) class Solution { public: From 0e552110ef57d4037076195eb61091bb077a0a0e Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Thu, 29 Aug 2024 10:22:59 +0900 Subject: [PATCH 8/9] fix: complexity of combination sum --- combination-sum/heozeop.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/combination-sum/heozeop.cpp b/combination-sum/heozeop.cpp index e7624ac73..27555c75b 100644 --- a/combination-sum/heozeop.cpp +++ b/combination-sum/heozeop.cpp @@ -1,5 +1,7 @@ -// Time Complexity: O(n^2) -// Spatial Complexity: O(n); +// Time Complexity: O(n^target); +// - target에 비례하는 tree 깊이에 따라 n번 순회 발생 +// Spatial Complexity: O(target); +// - target에 비례하는 visited vector, answer vector만 있으면 됨. class Solution { public: From 274e467c13cb87b11d34e0e8b425f1a0c082c5d9 Mon Sep 17 00:00:00 2001 From: Lee seung chan Date: Fri, 30 Aug 2024 08:03:22 +0900 Subject: [PATCH 9/9] refac: remove useless condition check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 굳이 필요없는 확인으로 의식적인 이해가 한개 더 필요한 상황 발생 --- coin-change/heozeop.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/coin-change/heozeop.cpp b/coin-change/heozeop.cpp index 68b53a1ae..6a173d6e5 100644 --- a/coin-change/heozeop.cpp +++ b/coin-change/heozeop.cpp @@ -20,10 +20,6 @@ class Solution { continue; } - if (dp[i - coin] == MAX_VALUE) { - continue; - } - dp[i] = min(1 + dp[i - coin], dp[i]); } }