Skip to content

[oyeong011] Week 1 #642

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 9 commits into from
Dec 14, 2024
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
14 changes: 14 additions & 0 deletions contains-duplicate/oyeong011.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map<int, int> mp;
for(int a : nums){
if(++mp[a] >= 2){
return true;
}
}
return false;
}
};
17 changes: 17 additions & 0 deletions house-robber/oyeong011.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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]);

vector<int> dp(n);
dp[0] = nums[0];
dp[1] = max(nums[0], nums[1]);
for(int i = 2; i < n; i++){
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]);
}
return dp[n - 1];
}
};
21 changes: 21 additions & 0 deletions longest-consecutive-sequence/oyeong011.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
const int INF = 987654321;
int temp = INF, ret = 0, cur = 0;

sort(nums.begin(), nums.end());
for(int a : nums){
if(a == temp)continue;
if(temp == INF || temp + 1 == a){
cur++; temp = a;
} else {
ret = max(ret, cur);
cur = 1;
temp = a;
}
}
ret = max(ret, cur);
return ret;
}
};
16 changes: 16 additions & 0 deletions top-k-frequent-elements/oyeong011.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
map<int, int> mp;
priority_queue<pair<int, int>> pq;
vector<int> ans;

for(auto b : nums) mp[b]++;

for(auto p : mp) pq.push({p.second, p.first});

while(k--)ans.push_back(pq.top().second), pq.pop();

return ans;
}
};
16 changes: 16 additions & 0 deletions valid-palindrome/oyeong011.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
bool isPalindrome(string s) {
string clean = "";
for(char c : s) {
if(isalnum(c)) {
clean += tolower(c);
}
}

string reversed = clean;
reverse(reversed.begin(), reversed.end());

return clean == reversed;
}
};
Loading