-
-
Notifications
You must be signed in to change notification settings - Fork 235
[PDKhan] WEEK 01 solutions #1149
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b5e449c
contains-duplicate solution
PDKhan 0ddb3f9
two-sum solution
PDKhan 7708bda
top-k-frequent-elements solution
PDKhan 2f93acb
longest-consecutive-sequence solution
PDKhan 2939e2c
house-robber solution
PDKhan ba7b2a8
change space O(1)
PDKhan 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,15 @@ | ||
class Solution { | ||
public: | ||
bool containsDuplicate(vector<int>& nums) { | ||
unordered_map<int, int> map; | ||
|
||
for(int i = 0; i < nums.size(); i++){ | ||
map[nums[i]]++; | ||
|
||
if(map[nums[i]] == 2) | ||
return true; | ||
} | ||
|
||
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,22 @@ | ||
class Solution { | ||
public: | ||
int rob(vector<int>& nums) { | ||
int n = nums.size(); | ||
|
||
if(n == 0) return 0; | ||
if(n == 1) return nums[0]; | ||
if(n == 2) return max(nums[0], nums[1]); | ||
|
||
int prev2 = nums[0]; | ||
int prev1 = max(nums[0], nums[1]); | ||
|
||
|
||
for(int i = 2; i < n; i++){ | ||
int curr = max(prev1, prev2 + nums[i]); | ||
prev2 = prev1; | ||
prev1 = curr; | ||
} | ||
|
||
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,30 @@ | ||
class Solution { | ||
public: | ||
int longestConsecutive(vector<int>& nums) { | ||
int cnt = 1; | ||
int max = 0; | ||
|
||
if(nums.size() == 0) | ||
return 0; | ||
|
||
sort(nums.begin(), nums.end()); | ||
|
||
for(int i = 1; i < nums.size(); i++){ | ||
if(nums[i] == nums[i-1] + 1) | ||
cnt++; | ||
else if(nums[i] == nums[i-1]) | ||
continue; | ||
else{ | ||
if(max < cnt) | ||
max = cnt; | ||
|
||
cnt = 1; | ||
} | ||
} | ||
|
||
if(max < cnt) | ||
return cnt; | ||
|
||
return max; | ||
} | ||
}; |
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,26 @@ | ||
class Solution { | ||
public: | ||
vector<int> topKFrequent(vector<int>& nums, int k) { | ||
unordered_map<int, int> map; | ||
vector<int> result; | ||
|
||
for(int i = 0; i < nums.size(); i++){ | ||
map[nums[i]]++; | ||
} | ||
|
||
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int>>> minHeap; | ||
|
||
for(auto i : map){ | ||
minHeap.push({i.second, i.first}); | ||
if(minHeap.size() > k) | ||
minHeap.pop(); | ||
} | ||
|
||
while(!minHeap.empty()){ | ||
result.push_back(minHeap.top().second); | ||
minHeap.pop(); | ||
} | ||
|
||
return result; | ||
} | ||
}; |
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 @@ | ||
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
unordered_map<int, int> Map; | ||
|
||
for(int i = 0; i < nums.size(); i++){ | ||
int diff = target - nums[i]; | ||
|
||
if(Map.find(diff) != Map.end()){ | ||
return { Map[diff], i }; | ||
} | ||
|
||
Map[nums[i]] = i; | ||
} | ||
|
||
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.
map에 추가하면서 2번 이상 추가된 요소면 종료 하는 방식이군요! 저는 추가를 완료하고 조회를 하도록 했는데 더 좋은 방식인 것 같습니다. 참고하겠습니다 👍
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.
저는 추가하면서 2번 이상이면 바로 종료가 되는 것이 더 빠르다고 판단했습니다.
근데 다른 분 리뷰하면서 보니 set 으로 이미 set 에 있으면 종료하는 방식도 있는데 이 것도 추천드립니다.
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.
오 좋은 방법인 것 같습니다. 저도 코드 한번 짜봐야겠네요! 감사합니다
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.
혹시 리뷰 완료가 가능하실지 문의 드립니다.
오늘까지 PR merge 완료해야 할듯 해서요~
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.
네넵 제가 승인 하는 걸 깜박했네요, 죄송합니다! 해두겠습니다!
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.
감사합니다!!!