-
-
Notifications
You must be signed in to change notification settings - Fork 304
[Sbeo-Joe] WEEK 01 solutions #1975
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class Solution { | ||
| public: | ||
| int map[101]; | ||
| 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; | ||
| } | ||
|
|
||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 {}; | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor) memo라는 이름을 사용하면 변수의 목적을 더 쉽게 확인할 수 있을 것 같아요!