diff --git a/contains-duplicate/kay30kim.cpp b/contains-duplicate/kay30kim.cpp new file mode 100644 index 000000000..f00d7c7c4 --- /dev/null +++ b/contains-duplicate/kay30kim.cpp @@ -0,0 +1,77 @@ +/* +Clarifications +1) Is this sorted Array? +2) What is the maximum / minimum value in nums? + +Solutions +1) Brute force - two nested for loop +T(C): O(n^2) +S(C): O(1) +2) Sort - Mergesort or library sort(Merge+quick) +T(C) : O(NlogN) +S(C) : O(N) +3) Hash +T(C) : O(1) +S(C) : O(N) +*/ + +class Solution { +public: + // Solution 2-1) Library Sort + // bool containsDuplicate(vector& nums) { + // sort(nums.begin(), nums.end()); + // for (size_t i = 0; i < nums.size() - 1; i++) + // { + // if (nums[i] == nums[i + 1]) + // return true; + // } + // return false; + // } + + // Solution 2-2) Implement Sort + void mergeSort(vector& nums, int start, int end, vector& temp) + { + if (start >= end) + return; + int mid = (start + end) / 2; + mergeSort(nums, start, mid, temp); + mergeSort(nums, mid + 1, end, temp); + int i = start, j = mid + 1, k = start; + while (i <= mid && j <= end) + { + if (nums[i] < nums[j]) + temp[k++] = nums[i++]; + else + temp[k++] = nums[j++]; + } + while (i <= mid) + temp[k++] = nums[i++]; + while (j <= end) + temp[k++] = nums[j++]; + for (int p = start; p <= end; p++) + nums[p] = temp[p]; + } + // Solution 1-2) Library Sort + // bool containsDuplicate(vector& nums) { + // vector temp(nums.size(), 0); + // mergeSort(nums, 0, nums.size() - 1, temp); + // for (size_t i = 0; i < nums.size() - 1; i++) + // { + // if (nums[i] == nums[i + 1]) + // return true; + // } + // return false; + // } + + // solution 2 - hash + bool containsDuplicate(vector& nums) { + unordered_map hash; + for (size_t i = 0; i < nums.size(); i++) + { + if (hash.find(nums[i]) != hash.end()) + return true; + hash[nums[i]] = i; + } + return false; + } +}; diff --git a/house-robber/kay30kim.cpp b/house-robber/kay30kim.cpp new file mode 100644 index 000000000..219fc17c4 --- /dev/null +++ b/house-robber/kay30kim.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int rob(vector& nums) { + vector> dp(nums.size() + 1, vector(2, 0)); + dp[0][1] = nums[0]; + for (int i = 1; i < nums.size(); i++) + { + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]); + dp[i][1] = dp[i - 1][0] + nums[i]; + } + return max(dp[nums.size() - 1][0], dp[nums.size() - 1][1]); + } +}; +//T(C) : O(N) diff --git a/longest-consecutive-sequence/kay30kim.cpp b/longest-consecutive-sequence/kay30kim.cpp new file mode 100644 index 000000000..b7ab16a28 --- /dev/null +++ b/longest-consecutive-sequence/kay30kim.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int longestConsecutive(vector& nums) { + unordered_set isNum; + int ans = 0; + for (auto num : nums) + isNum.insert(num); + for (auto num : isNum) // (auto num : nums)하면 틀린다 일부러 시작하는 위치를 많이 넣는 테스트케이스 존재 + { + if (isNum.find(num - 1) == isNum.end() && isNum.find(num) !=isNum.end()) + { + int curLen = 0; + for (int seqNum = num; isNum.find(seqNum) != isNum.end(); seqNum++) + { + curLen += 1; + } + ans = max(ans, curLen); + } + } + return ans; + } +}; diff --git a/top-k-frequent-elements/kay30kim.cpp b/top-k-frequent-elements/kay30kim.cpp new file mode 100644 index 000000000..0212e8f5f --- /dev/null +++ b/top-k-frequent-elements/kay30kim.cpp @@ -0,0 +1,56 @@ +// 1) Sort -> val, cnt -> +// T(C) : O(NlogN) +// 2) Heap +// T(C) : O(NlogN) +// 3) Bucket Sort +// T(C) : O(N) +class Solution { +private: + struct Node + { + int val; + int cnt; + bool operator<(const Node &input) const + { + return cnt < input.cnt; + } + }; +public: + // Solution 2 - Heap + // vector topKFrequent(vector& nums, int k) { + // priority_queue pq; + // unordered_map numCnt; + // vector topKvalueVec; + // for(auto num : nums) + // numCnt[num] += 1; + // for(auto it : numCnt) + // pq.push({it.first, it.second}); + // for (int i = 0; i < k && !pq.empty(); i++) + // { + // topKvalueVec.push_back(pq.top().val); + // pq.pop(); + // } + // return topKvalueVec; + // } + + // Solution 3 - Bucket Sort + vector topKFrequent(vector& nums, int k) { + unordered_map numToCnt; + vector> freq(nums.size() + 1, vector()); + vector ans; + for (int i = 0; i < nums.size(); i++) + numToCnt[nums[i]] += 1; + for (auto it : numToCnt) + freq[it.second].push_back(it.first); + for (int cnt = nums.size(); cnt >= 0; cnt--) + { + for (auto num : freq[cnt]) + { + ans.push_back(num); + if (ans.size() >= k) + return ans; + } + } + return ans; + } +}; diff --git a/two-sum/kay30kim.cpp b/two-sum/kay30kim.cpp new file mode 100644 index 000000000..abbf5f042 --- /dev/null +++ b/two-sum/kay30kim.cpp @@ -0,0 +1,26 @@ +// 1) Brute Force - two nested for loop +// T(C) : O(N^2) +// 2) sort & two pointer +// T(C) : O(NlogN) +// 3) hash +// T(C) : O(N) + + +class Solution { +public: + vector twoSum(vector& nums, int target) { + vector indicesVector; + unordered_map numToIdx; + for (int i = 0; i < nums.size(); i++) + { + if(numToIdx.find(target - nums[i]) != numToIdx.end()) + { + indicesVector.push_back(numToIdx[target - nums[i]]); + indicesVector.push_back(i); + break; + } + numToIdx[nums[i]] = i; + } + return indicesVector; + } +};