Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions contains-duplicate/Hong-Study.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
std::unordered_map<int, int> map;
for(const auto& num: nums){
if(map.find(num) != map.end()){
return true;
}

map[num] = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map[num] = 1 로 value를 1로 지정하신 이유가 있을까요?
단순히 키 값의 존재 여부를 표시하기 위한 값일까요??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵넵 단순 키 값의 존재여부를 표시하기 위함입니다. 지금 생각해보면 map이 아닌 set을 써도 됬었네요 ㅎㅎ

}

return false;
}
};

20 changes: 20 additions & 0 deletions longest-consecutive-sequence/Hong-Study.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
std::set<int> sorts{nums.begin(), nums.end()};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미 알고 계실 수 있겠지만..
해당 문제 풀면서 알게된 내용인데요, sort 를 사용하면 시간 복잡도가 O(n log n) 이 나온다고 하더라고요.
set을 unordered로 사용해서 연속되는 숫자의 길이를 측정할 수 있도록 풀어보시는 것은 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한번 참고해서 해볼게요 ㅎㅎ 감사합니다!


// 개선 필요...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어떤 점이 개선이 필요할지, 고민되는 것이 무엇인지 알려주시면 리뷰어가 함께 고민해줄 수 있을 것 같아요~!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사이트에서 속도가 50% 이하로 나와서 시간 복잡도 개선이 필요하다는 내용이였습니다! 다음에는 참고하여 디테일하게 적어볼게요 ㅎㅎ

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

29 changes: 29 additions & 0 deletions top-k-frequent-elements/Hong-Study.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
std::unordered_map<int, int> maps;
for (const auto num : nums) {
if (maps.find(num) == maps.end())
maps[num] = 0;
maps[num] += 1;
}

std::vector<std::pair<int, int>> sorts{maps.begin(), maps.end()};
sort(sorts.begin(), sorts.end(),
[](std::pair<int, int> a, std::pair<int, int> b) {
return a.second > b.second;
});

std::vector<int> result;
int i = 0;
for (const auto m : sorts) {
result.push_back(m.first);
i++;
if (i == k)
break;
}

return result;
}
};

19 changes: 19 additions & 0 deletions two-sum/Hong-Study.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
std::map<int, int> 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<int>{idx, i};
}

return std::vector<int>{0, 0};
}
};