Skip to content

[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 6 commits into from
Apr 5, 2025
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
15 changes: 15 additions & 0 deletions contains-duplicate/PDKhan.cpp
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

map에 추가하면서 2번 이상 추가된 요소면 종료 하는 방식이군요! 저는 추가를 완료하고 조회를 하도록 했는데 더 좋은 방식인 것 같습니다. 참고하겠습니다 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

저는 추가하면서 2번 이상이면 바로 종료가 되는 것이 더 빠르다고 판단했습니다.
근데 다른 분 리뷰하면서 보니 set 으로 이미 set 에 있으면 종료하는 방식도 있는데 이 것도 추천드립니다.

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.

혹시 리뷰 완료가 가능하실지 문의 드립니다.
오늘까지 PR merge 완료해야 할듯 해서요~

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.

감사합니다!!!

return true;
}

return false;
}
};
22 changes: 22 additions & 0 deletions house-robber/PDKhan.cpp
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;
}
};
30 changes: 30 additions & 0 deletions longest-consecutive-sequence/PDKhan.cpp
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;
}
};
26 changes: 26 additions & 0 deletions top-k-frequent-elements/PDKhan.cpp
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;
}
};
18 changes: 18 additions & 0 deletions two-sum/PDKhan.cpp
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 {};
}
};