Skip to content

Commit 3ce42a0

Browse files
authored
Merge pull request #1975 from Sbeo-Joe/main
[Sbeo-Joe] WEEK 01 solutions
2 parents 3b35523 + 961f77a commit 3ce42a0

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

contains-duplicate/Sbeo-Joe.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool containsDuplicate(vector<int>& nums) {
4+
set<int> s;
5+
6+
for(const auto& n : nums) {
7+
auto iter = s.find(n);
8+
if(iter != s.end()){
9+
return true;
10+
}
11+
s.insert(n);
12+
}
13+
return false;
14+
}
15+
};

house-robber/Sbeo-Joe.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int map[101];
4+
int rob(vector<int>& nums) {
5+
6+
memset(map, -1, sizeof(map));
7+
int ret = -1;
8+
for(int i = 0; i < nums.size(); i++){
9+
ret = max(ret, solve(nums, i));
10+
}
11+
return ret;
12+
}
13+
14+
int solve(vector<int>& nums, int here) {
15+
if(here >= nums.size()) return 0;
16+
17+
int& ret = map[here];
18+
if(ret != -1) return ret;
19+
ret = nums[here];
20+
for(int there = here + 2; there < nums.size(); there++){
21+
ret = max(ret, solve(nums, there) + nums[here]);
22+
}
23+
return ret;
24+
}
25+
26+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int longestConsecutive(vector<int>& nums) {
4+
if(nums.size() == 0) return 0;
5+
set<int> us{nums.begin(), nums.end()};
6+
int ret = 1;
7+
vector<int> vec(us.begin(), us.end());
8+
int prevNum = vec[0];
9+
int ret_candi = 1;
10+
for(int i = 1; i < vec.size(); i++){
11+
if(vec[i] - vec[i-1] == 1){
12+
ret_candi++;
13+
ret = max(ret, ret_candi);
14+
continue;
15+
} else {
16+
ret_candi = 1;
17+
continue;
18+
}
19+
}
20+
21+
return ret;
22+
}
23+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
vector<int> topKFrequent(vector<int>& nums, int k) {
4+
// value, count
5+
unordered_map<int, int> um;
6+
for(auto& n : nums){
7+
um[n]++;
8+
}
9+
10+
//value, count
11+
vector<std::pair<int, int>> vec(um.begin(), um.end());
12+
sort(vec.begin(), vec.end(),
13+
[](const auto& a, const auto& b) { return a.second > b.second;});
14+
15+
vector<int> ret;
16+
for(int i=0; i< k; i++){
17+
ret.push_back(vec[i].first);
18+
}
19+
20+
return ret;
21+
}
22+
};

two-sum/Sbeo-Joe.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
5+
for(int i = 0; i < nums.size() - 1; i++){
6+
for(int j = i + 1; j < nums.size(); j++){
7+
if(nums[i] + nums[j] == target){
8+
return std::vector<int>{i, j};
9+
}
10+
}
11+
}
12+
return {};
13+
}
14+
};
15+
16+
class Solution {
17+
public:
18+
vector<int> twoSum(vector<int>& nums, int target) {
19+
// value, index;
20+
unordered_map<int, int> um;
21+
for(int i = 0; i < nums.size(); i++){
22+
int gap = target - nums[i];
23+
if(um.count(gap)){
24+
return {um[gap], i};
25+
}
26+
um[nums[i]] = i;
27+
}
28+
return {};
29+
}
30+
};

0 commit comments

Comments
 (0)