Skip to content
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/Sbeo-Joe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
set<int> s;

for(const auto& n : nums) {
auto iter = s.find(n);
if(iter != s.end()){
return true;
}
s.insert(n);
}
return false;
}
};
26 changes: 26 additions & 0 deletions house-robber/Sbeo-Joe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
int map[101];
Copy link
Contributor

Choose a reason for hiding this comment

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

minor) memo라는 이름을 사용하면 변수의 목적을 더 쉽게 확인할 수 있을 것 같아요!

int rob(vector<int>& nums) {

memset(map, -1, sizeof(map));
int ret = -1;
for(int i = 0; i < nums.size(); i++){
ret = max(ret, solve(nums, i));
}
return ret;
}

int solve(vector<int>& nums, int here) {
if(here >= nums.size()) return 0;

int& ret = map[here];
if(ret != -1) return ret;
ret = nums[here];
for(int there = here + 2; there < nums.size(); there++){
ret = max(ret, solve(nums, there) + nums[here]);
}
return ret;
}

};
23 changes: 23 additions & 0 deletions longest-consecutive-sequence/Sbeo-Joe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.size() == 0) return 0;
set<int> us{nums.begin(), nums.end()};
Copy link
Contributor

Choose a reason for hiding this comment

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

us라는 변수명이 좀 더 의미를 가지면 좋을 것 같습니다!

int ret = 1;
vector<int> vec(us.begin(), us.end());
int prevNum = vec[0];
int ret_candi = 1;
for(int i = 1; i < vec.size(); i++){
if(vec[i] - vec[i-1] == 1){
ret_candi++;
ret = max(ret, ret_candi);
continue;
} else {
ret_candi = 1;
continue;
}
}

return ret;
}
};
22 changes: 22 additions & 0 deletions top-k-frequent-elements/Sbeo-Joe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
// value, count
unordered_map<int, int> um;
for(auto& n : nums){
um[n]++;
}

//value, count
vector<std::pair<int, int>> vec(um.begin(), um.end());
sort(vec.begin(), vec.end(),
[](const auto& a, const auto& b) { return a.second > b.second;});

vector<int> ret;
for(int i=0; i< k; i++){
ret.push_back(vec[i].first);
}

return ret;
}
};
30 changes: 30 additions & 0 deletions two-sum/Sbeo-Joe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {

for(int i = 0; i < nums.size() - 1; i++){
for(int j = i + 1; j < nums.size(); j++){
if(nums[i] + nums[j] == target){
return std::vector<int>{i, j};
}
}
}
return {};
}
};

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
// value, index;
unordered_map<int, int> um;
for(int i = 0; i < nums.size(); i++){
int gap = target - nums[i];
if(um.count(gap)){
return {um[gap], i};
}
um[nums[i]] = i;
}
return {};
}
};