-
-
Notifications
You must be signed in to change notification settings - Fork 195
[crumbs22] WEEK 1 #1188
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
[crumbs22] WEEK 1 #1188
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
935bc37
FEAT : upload #217
crumbs22 13719f3
CHORE : add a newline
crumbs22 b155433
FEAT : upload #219
crumbs22 3337224
FEAT : upload #237.cpp
crumbs22 09bab7e
FEAT : solve #128
crumbs22 9437c60
FEAT : solve #264
crumbs22 b8b3e5f
FEAT : add comment
crumbs22 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,22 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <unordered_set> | ||
|
||
using namespace std; | ||
|
||
/* | ||
TC: O(n) | ||
벡터 nums의 모든 요소를 해시 테이블에 삽입하는 과정에서 O(n)만큼 소요됨 | ||
SC: O(n) | ||
|
||
풀이 방법 : nums의 중복요소를 거른 uset을 만들고, | ||
nums와 uset의 크기가 서로 다르다면 nums에 중복 요소가 하나 이상 존재한다. | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
bool containsDuplicate(vector<int>& nums) { | ||
unordered_set<int> uset(nums.begin(), nums.end()); | ||
return (nums.size() != uset.size()); | ||
} | ||
}; |
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,38 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
/* | ||
TC: O(n) | ||
SC: O(n) | ||
|
||
풀이 방법: 벡터 nums가 비어있을 때부터 시작해 단계적으로 nums의 마지막 요소까지 올라가며 dp 배열을 만든다. | ||
단 nums가 비어있으면 0을 반환하고, nums에 하나만 있다면 해당 값을 반환하며 | ||
nums에 2개가 있을 시엔 두 값 중 큰 값을 반환한다 | ||
dp[i]는 i번째 집까지 털었을 때 얻을 수 있는 최대 금액을 저장한다 | ||
- 점화식 : dp[i] = max(dp[i-1], dp[i-2] + nums[i]) | ||
*/ | ||
class Solution { | ||
public: | ||
int rob(vector<int>& nums) { | ||
vector<int> dp; | ||
int cnt; | ||
|
||
if (nums.size() == 0) | ||
return (0); | ||
if (nums.size() == 1) | ||
return (nums.back()); | ||
if (nums.size() == 2) | ||
return (nums.front() > nums.back() ? nums.front() : nums.back()); | ||
|
||
dp.push_back(0); | ||
dp.push_back(nums.front()); | ||
for (int i = 2; i - 1 < nums.size(); i++) | ||
{ | ||
dp[i - 2] + nums[i - 1] > dp[i - 1] ? \ | ||
cnt = dp[i - 2] + nums[i - 1] : cnt = dp[i - 1]; | ||
dp.push_back(cnt); | ||
} | ||
return (dp.back()); | ||
} | ||
}; |
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,41 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <set> | ||
|
||
using namespace std; | ||
|
||
/* | ||
TC: O(nlogn) | ||
- 정렬된 벡터 s 생성: O(nlogn) ... std::set은 이진 검색 트리를 기반으로 한다 | ||
- s의 순회: O(n) | ||
=> 전체 시간 복잡도: O(nlogn) | ||
SC: O(n) | ||
|
||
풀이 방법: nums에 대한 정렬된 벡터 s의 루프를 돌면서 | ||
연속되는 다음 값을 변수 i로 두고 i값과 비교해서 연속되지 않는다면 다시 1부터 최장길이를 센다. | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int longestConsecutive(vector<int>& nums) { | ||
set<int> s(nums.begin(), nums.end()); // set은 정렬된 벡터이다 | ||
int cnt; | ||
int max = 0; | ||
|
||
int i = *s.begin(); // 초기 비교값은 테이블의 첫번째 값으로 둔다 | ||
for (set<int>::iterator iter = s.begin(); iter != s.end(); iter++) | ||
{ | ||
if (*iter != i) // 테이블의 현재 값이 연속되어야 하는 값이 아닌 경우 | ||
{ | ||
cnt = 1; | ||
i = *iter + 1; | ||
continue; | ||
} | ||
cnt++; | ||
i++; | ||
if (cnt > max) | ||
max = cnt; | ||
} | ||
return (max); | ||
} | ||
}; |
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,51 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <unordered_map> | ||
#include <sort> | ||
|
||
using namespace std; | ||
|
||
/* | ||
TC: O(nlogn) | ||
- 해시맵에 요소 삽입: O(n) | ||
- 해시맵을 벡터로 변환: O(n) | ||
- 벡터 정렬: O(nlogn) ... sort() | ||
=> 전체 시간 복잡도는 O(nlogn) | ||
SC: O(n) | ||
- 해시맵 저장 공간: O(n) | ||
- 벡터 변환 공간: O(n) | ||
=> 전체 공간 복잡도는 O(n) | ||
|
||
풀이 방법: nums를 순회하며 각 숫자의 빈도를 해시맵 umap에 저장하고, 해시맵을 pair 형태의 벡터로 변환한다 | ||
그리고 umap을 빈도기준으로 정렬하고 상위 k개 숫자를 추출해서 반환한다 | ||
*/ | ||
|
||
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]); // umap에 nums[i]가 존재하는지 탐색 | ||
if (tmp != umap.end()) // 이미 존재한다면 개수 + 1 하고 | ||
tmp->second += 1; | ||
else // 존재하지 않는다면 nums[i]를 umap에 삽입한다 | ||
umap.insert(make_pair(nums[i], 1)); | ||
} | ||
vector<pair<int,int>> vec(umap.begin(), umap.end()); // map을 vector로 이동 | ||
sort(vec.begin(), vec.end(), cmp); // vec를 개수에 따라 오름차순 정렬해준다 | ||
|
||
vector<int> result; | ||
for (int i = 0; i < k; i++) // 정렬된 vec배열의 앞에서부터 k개까지 탐색해서 반환한다 | ||
result.push_back(vec[i].first); | ||
return (result); | ||
} | ||
}; |
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 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
/* | ||
TC: O(n^2) | ||
- 외부 루프: 벡터 nums의 모든 요소를 순회 (O(n)) | ||
- 내부 루프: 현재 요소 이후의 모든 요소를 탐색하여 complement와 일치하는 값을 찾음 (O(n)) | ||
=> 전체 시간 복잡도는 O(n^2) | ||
|
||
SC: O(1) | ||
|
||
풀이 방법: 각 요소에 대해 타겟에서 해당 요소를 뺀 값을(complement) 계산하고, 나머지 요소들 중에서 해당 값을 탐색한다. | ||
*/ | ||
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
int complement; | ||
if (nums.size() == 2) | ||
return {1, 2}; | ||
|
||
for (int i = 0; i < nums.size(); i++) | ||
{ | ||
complement = target - nums[i]; | ||
for (int j = i + 1; j < nums.size(); j++) | ||
{ | ||
if (nums[j] == complement) | ||
{ | ||
return {i, j}; | ||
} | ||
} | ||
} | ||
return {-1}; | ||
} | ||
}; |
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.
이중 for loop를 사용하고 있는데 여기서 해시맵을 사용해서 시간복잡도를 줄이는 방법을 생각해보면 좋을 것 같아요!
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.
리뷰 감사합니다! 다시 고려해서 풀어보도록 하겠습니다. 다음 주차도 화이팅!