-
-
Notifications
You must be signed in to change notification settings - Fork 302
[dylan-jung] WEEK 01 solutions #1984
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
+103
−0
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3feb455
contains duplicated solution
dylan-jung 83f9b70
fix: contains duplicated solution
dylan-jung 8c86a4c
two-sum solution
dylan-jung 0d8c4b8
top-k-freq-elems solutions
dylan-jung 394d5f3
longest-consecutive-sequence solution
dylan-jung a51dcca
house-robber solution
dylan-jung f01aeb3
fix two some
dylan-jung 4aad6ff
fix add comment at contains duplicate
dylan-jung 844a96d
fix top-k-freq-elements
dylan-jung f297013
add comment on longest-consecutive-sequence
dylan-jung d958263
fix: house-robber
dylan-jung 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,13 @@ | ||
| // TC: O(N), SC: O(N) | ||
|
|
||
| class Solution { | ||
| public: | ||
| bool containsDuplicate(vector<int>& nums) { | ||
| unordered_set<int> s; | ||
| for(auto item: nums) { | ||
| if(s.count(item) > 0) return true; | ||
| s.insert(item); | ||
| } | ||
| return false; | ||
| } | ||
| }; |
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,18 @@ | ||
| // TC: O(N), SC: O(1) | ||
| class Solution { | ||
| public: | ||
| int rob(vector<int>& nums) { | ||
| if (nums.size() == 1) return nums[0]; | ||
|
|
||
| int prev2 = nums[0]; // dp[i-2] | ||
| int prev1 = max(nums[0], nums[1]); // dp[i-1] | ||
|
|
||
| for (int i = 2; i < nums.size(); i++) { | ||
| int cur = max(prev1, prev2 + nums[i]); | ||
| prev2 = prev1; | ||
| prev1 = cur; | ||
| } | ||
|
|
||
| return prev1; | ||
| } | ||
| }; |
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 @@ | ||
| // TC: O(N), SC: O(N) | ||
|
|
||
| class Solution { | ||
| public: | ||
| int longestConsecutive(vector<int>& nums) { | ||
| auto s = unordered_set<int>(); | ||
| for(int it : nums) { | ||
| s.insert(it); | ||
| } | ||
|
|
||
| int m = 0; | ||
| for(int it : s) { | ||
| if(s.count(it-1) > 0) continue; | ||
|
|
||
| int cnt = 0; | ||
| while(s.count(it+cnt) > 0){ | ||
| cnt++; | ||
| } | ||
| m = max(m, cnt); | ||
| } | ||
|
|
||
| return m; | ||
| } | ||
| }; |
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,31 @@ | ||
| // TC: O(N), SC: O(N) | ||
| class Solution { | ||
| public: | ||
| vector<int> topKFrequent(vector<int>& nums, int k) { | ||
| unordered_map<int, int> freq; | ||
| freq.reserve(nums.size() * 2); | ||
| for (int x : nums) { | ||
| ++freq[x]; | ||
| } | ||
|
|
||
| int n = nums.size(); | ||
| vector<vector<int>> bucket(n + 1); | ||
| for (auto& p : freq) { | ||
| int num = p.first; | ||
| int cnt = p.second; | ||
| bucket[cnt].push_back(num); | ||
| } | ||
|
|
||
| vector<int> ans; | ||
| ans.reserve(k); | ||
|
|
||
| for (int count = n; count >= 1 && ans.size() < k; --count) { | ||
| for (int num : bucket[count]) { | ||
| ans.push_back(num); | ||
| if (ans.size() == k) break; | ||
| } | ||
| } | ||
|
|
||
| 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,17 @@ | ||
| // TC: O(N), SC: O(N) | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<int> twoSum(vector<int>& nums, int target) { | ||
| unordered_map<int, int> m; | ||
| for(int i = 0; i < nums.size(); i++) { | ||
| m[nums[i]] = i; | ||
| } | ||
| for(int first = 0; first < nums.size(); first++) { | ||
| if(m.count(target - nums[first]) > 0 && first != m[target - nums[first]]) { | ||
| return {first, m[target - nums[first]]}; | ||
| } | ||
| } | ||
| return {}; | ||
| } | ||
| }; | ||
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.
좋은 풀이인데요! 복잡도가 nlogn이라 복잡도 n인 해시맵 풀이법도 해보시면 좋을 것 같습니답!