diff --git a/contains-duplicate/Hong-Study.cpp b/contains-duplicate/Hong-Study.cpp new file mode 100644 index 0000000000..e9b624b678 --- /dev/null +++ b/contains-duplicate/Hong-Study.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + bool containsDuplicate(vector& nums) { + std::unordered_map map; + for(const auto& num: nums){ + if(map.find(num) != map.end()){ + return true; + } + + map[num] = 1; + } + + return false; + } +}; + diff --git a/longest-consecutive-sequence/Hong-Study.cpp b/longest-consecutive-sequence/Hong-Study.cpp new file mode 100644 index 0000000000..a3fec66872 --- /dev/null +++ b/longest-consecutive-sequence/Hong-Study.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int longestConsecutive(vector& nums) { + std::set sorts{nums.begin(), nums.end()}; + + // 개선 필요... + int maxSequence = 0, sequence = 0, i = -1; + for (const auto& num : sorts) { + if (sorts.find(num + 1) != sorts.end()) { + sequence += 1; + } else { + maxSequence = std::max(maxSequence, sequence + 1); + sequence = 0; + } + } + + return maxSequence; + } +}; + diff --git a/top-k-frequent-elements/Hong-Study.cpp b/top-k-frequent-elements/Hong-Study.cpp new file mode 100644 index 0000000000..ebe4cc13df --- /dev/null +++ b/top-k-frequent-elements/Hong-Study.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + std::unordered_map maps; + for (const auto num : nums) { + if (maps.find(num) == maps.end()) + maps[num] = 0; + maps[num] += 1; + } + + std::vector> sorts{maps.begin(), maps.end()}; + sort(sorts.begin(), sorts.end(), + [](std::pair a, std::pair b) { + return a.second > b.second; + }); + + std::vector result; + int i = 0; + for (const auto m : sorts) { + result.push_back(m.first); + i++; + if (i == k) + break; + } + + return result; + } +}; + diff --git a/two-sum/Hong-Study.cpp b/two-sum/Hong-Study.cpp new file mode 100644 index 0000000000..f03ca5612c --- /dev/null +++ b/two-sum/Hong-Study.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + std::map numMap; + for (int i = 0; i < nums.size(); i++) { + int leftNum = target - nums[i]; + if (numMap.find(leftNum) == numMap.end()) { + numMap[nums[i]] = i; + continue; + } + + int idx = numMap[leftNum]; + return std::vector{idx, i}; + } + + return std::vector{0, 0}; + } +}; +