-
-
Notifications
You must be signed in to change notification settings - Fork 305
[Hong-Study] WEEK 01 solutions #1999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| 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()}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이미 알고 계실 수 있겠지만..
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 한번 참고해서 해볼게요 ㅎㅎ 감사합니다! |
||
|
|
||
| // 개선 필요... | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어떤 점이 개선이 필요할지, 고민되는 것이 무엇인지 알려주시면 리뷰어가 함께 고민해줄 수 있을 것 같아요~!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| }; | ||
|
|
||
| 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; | ||
| } | ||
| }; | ||
|
|
| 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}; | ||
| } | ||
| }; | ||
|
|
There was a problem hiding this comment.
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로 지정하신 이유가 있을까요?
단순히 키 값의 존재 여부를 표시하기 위한 값일까요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵넵 단순 키 값의 존재여부를 표시하기 위함입니다. 지금 생각해보면 map이 아닌 set을 써도 됬었네요 ㅎㅎ