-
-
Notifications
You must be signed in to change notification settings - Fork 195
[haung921209] WEEK 01 solutions #1180
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
10 commits
Select commit
Hold shift + click to select a range
f2a914e
commit
haung921209 56110ac
줄바꿈 추가
haung921209 a120aa8
줄바꿈 추가
haung921209 7f3667e
주석 추가
haung921209 49ac3ca
Update haung921209.md
haung921209 847f7b4
Update haung921209.md
haung921209 a6c8ad4
Update haung921209.md
haung921209 a463f9a
Update haung921209.md
haung921209 f5ffdca
Update haung921209.md
haung921209 46b2884
Update haung921209.md
haung921209 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,37 @@ | ||
|
||
|
||
# cpp stl | ||
```cpp | ||
class Solution { | ||
public: | ||
bool containsDuplicate(vector<int>& nums) { | ||
set<int> numSet(nums.begin(), nums.end()); | ||
return numSet.size() != nums.size(); | ||
} | ||
}; | ||
``` | ||
|
||
- set으로 단순비교. 편리하나, 정렬에 비해 시간이 오래 걸림. | ||
- unordered_set을 쓰면 set 방식에서 조금 더 빠를 수는 있음 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
bool containsDuplicate(vector<int>& nums) { | ||
sort(nums.begin(), nums.end()); | ||
for(int i=0;i<nums.size()-1;i++){ | ||
if(nums[i]==nums[i+1]){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}; | ||
``` | ||
|
||
- 둘 다 O(n logn)의 시간 복잡도이나, 자료 특성 상 정렬이 더 빠름 | ||
|
||
|
||
|
||
|
||
|
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,24 @@ | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int rob(vector<int>& nums) { | ||
if(nums.size()==1) | ||
return nums[0]; | ||
|
||
int res = max(nums[0], nums[1]); | ||
vector<int> sav(nums.size(), 0); | ||
sav[0] = nums[0]; | ||
sav[1] = res; | ||
for(int i=2;i<nums.size();i++){ | ||
sav[i] = max(sav[i-1], sav[i-2]+nums[i]); | ||
res = max(res, sav[i]); | ||
} | ||
return res; | ||
} | ||
}; | ||
``` | ||
|
||
- 1차원 dp | ||
|
||
|
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,35 @@ | ||
```cpp | ||
class Solution { | ||
public: | ||
int longestConsecutive(vector<int>& nums) { | ||
if(nums.size()<=1){ | ||
return nums.size(); | ||
} | ||
priority_queue<int> pq(nums.begin(), nums.end()); | ||
int cnt = 1; | ||
int maxCnt = 1; | ||
int before = pq.top();pq.pop(); | ||
while(pq.size()>0){ | ||
int cur = pq.top(); pq.pop(); | ||
if(before-cur ==1){ | ||
cnt++; | ||
maxCnt = max(maxCnt, cnt); | ||
}else if(before==cur){ | ||
continue; | ||
}else{ | ||
maxCnt = max(maxCnt, cnt); | ||
cnt=1; | ||
} | ||
before = cur; | ||
} | ||
|
||
return maxCnt; | ||
} | ||
}; | ||
``` | ||
|
||
- 순서 유지 조건 없으므로, insert와 정렬을 동시에 할 수 있는 우선순위 큐 사용 | ||
- 전체 정렬이 필요할 경우 정렬이 더 유리할 수 있으나, 최적화 여지가 우선순위 큐가 더 커서 사용 | ||
- ex) 탐색 중단 조건 등 | ||
|
||
|
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 @@ | ||
```cpp | ||
class Solution { | ||
public: | ||
vector<int> topKFrequent(vector<int>& nums, int k) { | ||
unordered_map<int, int> freqCntMp; | ||
vector<vector<int>> v(nums.size()+1, vector<int>()); | ||
int maxSize = 0; | ||
for(int i=0;i<nums.size();i++){ | ||
freqCntMp[nums[i]]+=1; | ||
v[freqCntMp[nums[i]]].push_back(nums[i]); | ||
if(maxSize<freqCntMp[nums[i]]) | ||
maxSize = freqCntMp[nums[i]]; | ||
} | ||
for(int idx = maxSize;idx>0;idx--){ | ||
if(v[idx].size()==k){ | ||
return v[idx]; | ||
} | ||
} | ||
return vector<int>(); | ||
} | ||
}; | ||
``` | ||
|
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,46 @@ | ||
## 단순 순회 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
int i=0,j=1; | ||
for(i=0;i<j;i++){ | ||
for(j=i+1;j<nums.size();j++){ | ||
if(nums[i]+nums[j]==target){ | ||
return {i, j}; | ||
} | ||
} | ||
} | ||
return {0, 1}; | ||
|
||
} | ||
}; | ||
``` | ||
|
||
- O(n^2) | ||
|
||
## stl 사용 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
unordered_map<int, int> valIdxMap; | ||
|
||
for(int idx=0;idx<nums.size();idx++){ | ||
if(valIdxMap.find(target-nums[idx]) != valIdxMap.end()){ | ||
return {idx, valIdxMap[target-nums[idx]]}; | ||
} | ||
valIdxMap[nums[idx]] = idx; | ||
|
||
} | ||
return {0, 1}; | ||
|
||
} | ||
}; | ||
``` | ||
|
||
- O(nlogn) | ||
|
||
|
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.
unordered_set을 쓰면 O(n)으로 더 빠를 수 있으나 장단점을 고려해 정렬로 풀은 점 배워갑니다. 비교해보는 것 좋네요!