Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions contains-duplicate/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> uset(nums.begin(), nums.end());
return (nums.size() != uset.size());
}
};
29 changes: 29 additions & 0 deletions top-k-frequent-elements/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
bool cmp(const pair<int, int>& a, const pair<int, int>& b)
{
if (a.second == b.second)
return a.first < b.first;
return a.second > b.second;
}

class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> umap;

for (int i = 0; i < nums.size(); i++)
{
auto tmp = umap.find(nums[i]);
if (tmp != umap.end())
tmp->second += 1;
else
umap.insert(make_pair(nums[i], 1));
}
vector<pair<int,int>> vec(umap.begin(), umap.end()); // map을 vector로 이동
sort(vec.begin(), vec.end(), cmp);

vector<int> result;
for (int i = 0; i < k; i++)
result.push_back(vec[i].first);
return (result);
}
};
26 changes: 26 additions & 0 deletions two-sum/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
if (nums.size() == 2)
return {1, 2};
int tmp;
for (int i = 0; i < nums.size(); i++)
Copy link
Contributor

Choose a reason for hiding this comment

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

이중 for loop를 사용하고 있는데 여기서 해시맵을 사용해서 시간복잡도를 줄이는 방법을 생각해보면 좋을 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

리뷰 감사합니다! 다시 고려해서 풀어보도록 하겠습니다. 다음 주차도 화이팅!

{
tmp = target - nums[i];
for (int j = i + 1; j < nums.size(); j++)
{
if (nums[j] == tmp)
{
return {i, j};
}
}
}
return {-1};
}
};