Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
11 changes: 11 additions & 0 deletions contains-duplicate/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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;
}
};
20 changes: 20 additions & 0 deletions house-robber/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int dp[100] = { 0 };
int rob(vector<int>& nums) {
dp[0] = nums[0];
if(nums.size() >= 2) {
dp[1] = nums[1];
}
for(int i = 2; i < nums.size(); i++) {
for(int j = 0; j < i-1; j++) {
dp[i] = max(dp[j] + nums[i], dp[i]);
}
}
int m = -1;
for(int i = 0; i < nums.size(); i++) {
m = max(dp[i], m);
}
return m;
}
};
22 changes: 22 additions & 0 deletions longest-consecutive-sequence/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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;
}
};
23 changes: 23 additions & 0 deletions top-k-frequent-elements/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> m;
map<int, vector<int>> freq;
for(auto i: nums) m[i] += 1;
for(auto item: m) {
auto [k, v] = item;
freq[v].push_back(k);
}

vector<int> ans;
ans.reserve(k);
auto it = freq.rbegin();
while(k > 0) {
auto kth = it->second;
ans.insert(ans.end(), kth.begin(), kth.end());
k-=kth.size();
it++;
}
return ans;
}
};
18 changes: 18 additions & 0 deletions two-sum/dylan-jung.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) {
vector<tuple<int, int>> arr;
arr.reserve(nums.size());
for(int i = 0; i < nums.size(); i++) {
arr.push_back({nums[i], i});
}
int s = 0; int e = nums.size()-1;
sort(arr.begin(), arr.end());
while(s < e) {
if(get<0>(arr[s]) + get<0>(arr[e]) > target) e-=1;
else if (get<0>(arr[s]) + get<0>(arr[e]) < target) s+=1;
else return {get<1>(arr[s]), get<1>(arr[e])};
}
return {};
}
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

좋은 풀이인데요! 복잡도가 nlogn이라 복잡도 n인 해시맵 풀이법도 해보시면 좋을 것 같습니답!