-
-
Notifications
You must be signed in to change notification settings - Fork 337
[hwi-middle] WEEK 01 solutions #2373
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
Merged
+108
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class Solution { | ||
| public: | ||
| bool containsDuplicate(vector<int>& nums) { | ||
| sort(nums.begin(), nums.end()); | ||
| return nums.end() != unique(nums.begin(), nums.end()); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution { | ||
| public: | ||
| int rob(vector<int>& nums) { | ||
| int len = nums.size() + 1; | ||
| vector<int> d(len); | ||
| d[0] = 0; | ||
| d[1] = nums[0]; | ||
| for(int i = 2; i < len; ++i) | ||
| { | ||
| d[i] = max(d[i - 1], d[i - 2] + nums[i - 1]); | ||
| } | ||
|
|
||
| return d[len - 1]; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| class Solution { | ||
| public: | ||
| int longestConsecutive(vector<int>& nums) { | ||
| if (nums.empty()) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| unordered_set<int> s; | ||
| s.reserve(nums.size()); | ||
| s.max_load_factor(0.4f); | ||
| s.insert(nums.begin(), nums.end()); | ||
|
|
||
| int ans = 0; | ||
| for(auto& e : s) | ||
| { | ||
| if (s.contains(e - 1)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| int con = 1; | ||
| int num = e; | ||
| while (true) | ||
| { | ||
| num++; | ||
| if(!s.contains(num)) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| con++; | ||
| } | ||
| ans = max(ans, con); | ||
| } | ||
|
|
||
| return ans; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| class Solution { | ||
| public: | ||
| vector<int> topKFrequent(vector<int>& nums, int k) { | ||
| unordered_map<int, int> m; | ||
| for(auto n : nums) | ||
| { | ||
| ++m[n]; | ||
| } | ||
|
|
||
| vector<pair<int, int>> v(m.begin(), m.end()); | ||
| sort(v.begin(), v.end(), [](const auto& a, const auto& b) | ||
| { | ||
| return a.second > b.second; | ||
| }); | ||
|
|
||
| vector<int> ans; | ||
| ans.reserve(k); | ||
| for (int i = 0; i < k; ++i) | ||
| { | ||
| ans.push_back(v[i].first); | ||
| } | ||
| return ans; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| class Solution { | ||
| public: | ||
| vector<int> twoSum(vector<int>& nums, int target) { | ||
| vector<int> v(2); | ||
| unordered_map<int, int> m; | ||
| for (int i = 0; i < nums.size(); ++i) | ||
| { | ||
| int num = nums[i]; | ||
| if (m.contains(target - num)) | ||
| { | ||
| v[0] = m[target - num]; | ||
| v[1] = i; | ||
| return v; | ||
| } | ||
|
|
||
| m[num] = i; | ||
| } | ||
|
|
||
| v[0] = -1; | ||
| v[1] = -1; | ||
| return v; | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
저도 비슷한 방식으로 풀었는데요! 찾아보니 이 문제는 bucket sort 풀이도 많이 사용되는 접근이라고 하더라고요.
핵심 아이디어는
숫자 → 등장 횟수를 계산해서 map을 만드는 것까지는 동일하고, 이후 정렬을 하는 대신 등장 횟수를 인덱스로 사용하는 bucket 배열을 만드는 방식이라고 합니다.nums 길이만큼의 bucket 배열을 만들고 map을 순회하면서 각 숫자를 해당 빈도의 bucket에 넣은 뒤, bucket을 뒤에서부터 순회하면서 상위 k개의 숫자를 추출하는 아이디어라네요!
현재 풀이에서는
(num, frequency)를 정렬하는 단계 때문에 시간복잡도가 O(N log N)이 되는데, bucket sort를 사용하면 빈도 계산 O(N) + bucket 순회 O(N) 정도로 처리할 수 있어서 전체 시간복잡도를 O(N)으로 줄일 수 있다고 합니다.다만 지금 풀이도 충분히 깔끔하고 이해하기 좋은 접근인 것 같습니다 ㅎㅎ 좀 더 최적화할 수 있는 아이디어를 공유해봤습니다.
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.
공유해주셔서 감사합니다. 해당 풀이도 공부 기록으로 남겨두어야겠네요!