-
-
Notifications
You must be signed in to change notification settings - Fork 245
[haung921209] WEEK 02 solutions #1248
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 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2bf8fc0
initial commit
haung921209 1f30af4
Merge remote-tracking branch 'origin/main'
haung921209 f67b65a
climbing chairs 풀이
haung921209 222658e
product of array except self PR
haung921209 8f38943
Merge branch 'DaleStudy:main' into main
haung921209 0cc4f13
commit
haung921209 25b7e8f
Merge remote-tracking branch 'origin/main'
haung921209 e9805b0
commit
haung921209 9fc7481
개행 문자 추가
haung921209 f2f33c7
개행 문자 추가
haung921209 d7b599a
개행 문자 추가
haung921209 a34e806
Update .gitignore
haung921209 b42a095
Update .gitignore
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.vscode/ | ||
.DS_Store | ||
.env | ||
**/**-template.md | ||
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,67 @@ | ||
```cpp | ||
class Solution { | ||
public: | ||
vector<vector<int>> threeSum(vector<int>& nums) { | ||
sort(nums.begin(), nums.end()); | ||
set<vector<int>> res; | ||
for(int i=0;i<nums.size();i++){ | ||
int l = i+1, r = nums.size()-1; | ||
|
||
while(l<r){ | ||
int sum = nums[i]+nums[l]+nums[r]; | ||
|
||
if(sum<0){ | ||
l++; | ||
}else if(sum>0){ | ||
r--; | ||
}else{ | ||
res.insert({nums[i], nums[l], nums[r]}); | ||
l++; | ||
r--; | ||
} | ||
} | ||
} | ||
|
||
return vector<vector<int>>(res.begin(), res.end()); | ||
} | ||
}; | ||
``` | ||
|
||
- set -> vector 사용 이유는 중복 제거를 위함 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<vector<int>> threeSum(vector<int>& nums) { | ||
sort(nums.begin(), nums.end()); | ||
set<vector<int>> res; | ||
for(int i=0;i<nums.size();i++){ | ||
if(i != 0 && nums[i] == nums[i-1]) continue; | ||
|
||
int l = i+1, r = nums.size()-1; | ||
|
||
while(l<r){ | ||
int sum = nums[i]+nums[l]+nums[r]; | ||
|
||
if(sum<0){ | ||
l++; | ||
}else if(sum>0){ | ||
r--; | ||
}else{ | ||
res.insert({nums[i], nums[l], nums[r]}); | ||
l++; | ||
r--; | ||
} | ||
} | ||
} | ||
|
||
return vector<vector<int>>(res.begin(), res.end()); | ||
|
||
} | ||
}; | ||
``` | ||
|
||
- `if(i != 0 && nums[i] == nums[i-1]) continue;` 를 통한 탐색 범위 줄이기 최적화 정도의 차이로 상 / 하위 갈리는 정도 | ||
- 단순 2 pointer로 처리해도 무방 | ||
|
||
|
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 @@ | ||
```cpp | ||
class Solution { | ||
public: | ||
int climbStairs(int n) { | ||
vector<int> dp = {0, 1, 2}; | ||
dp.resize(n+1); | ||
for(int i=3;i<=n;i++){ | ||
dp[i] = dp[i-1] + dp[i-2]; | ||
} | ||
return dp[n]; | ||
} | ||
}; | ||
``` | ||
- O(n) | ||
- 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,44 @@ | ||
- URL : https://leetcode.com/problems/product-of-array-except-self/description/ | ||
- points from constraints | ||
- 2 <= nums.length <= 10^5 | ||
- if not use O(n) algorithm, a TLE occurs | ||
- -30 <= nums[i] <= 30 | ||
- the production result can be negative | ||
- do not use an unsigned type for the result object. | ||
- The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer. | ||
- There is no need to use a 64-bit (or larger) type for the result object. | ||
- However, it is not guaranteed that the intermediate object will always be a 32-bit type. | ||
|
||
|
||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<int> productExceptSelf(vector<int>& nums) { | ||
vector<int> idxOfZero; | ||
long long productRes = 1; | ||
for(int i=0;i<nums.size();i++){ | ||
if(nums[i]==0){ | ||
idxOfZero.push_back(i); | ||
}else{ | ||
productRes *=nums[i]; | ||
} | ||
} | ||
vector<int> res(nums.size(), 0); | ||
if(idxOfZero.size()>=2){ | ||
return res; | ||
}else if(idxOfZero.size()==1){ | ||
res[idxOfZero[0]] = productRes; | ||
return res; | ||
} | ||
|
||
for(int i=0;i<nums.size();i++){ | ||
res[i] = (int)(productRes / nums[i]); | ||
} | ||
return res; | ||
} | ||
}; | ||
``` | ||
|
||
- O(n) | ||
- long long type for result object -> 64bit(by constraint #3) | ||
haung921209 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
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,70 @@ | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
bool isAnagram(string s, string t) { | ||
if (s.length() != t.length()) | ||
return false; | ||
sort(s.begin(), s.end()); | ||
sort(t.begin(), t.end()); | ||
return s==t; | ||
} | ||
}; | ||
``` | ||
|
||
- O(nlogn) | ||
- std의 sort를 사용 | ||
|
||
``` | ||
class Solution { | ||
public: | ||
bool isAnagram(string s, string t) { | ||
if (s.length() != t.length()) | ||
return false; | ||
unordered_map<char, int> dict; | ||
for(auto c:s){ | ||
dict[c]++; | ||
} | ||
for(auto c:t){ | ||
if(dict[c]==0){ | ||
return false; | ||
} | ||
dict[c]--; | ||
} | ||
return true; | ||
|
||
} | ||
}; | ||
``` | ||
|
||
- O(n)? => O(nlogn) | ||
- map insert에서 O(logn)씩 소요됨 | ||
- 위의 sort보다는 효율적일 수 있음 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
bool isAnagram(string s, string t) { | ||
if (s.length() != t.length()) | ||
return false; | ||
vector<int>cntVec(26, 0); | ||
for(auto c:s){ | ||
cntVec[(int(c-'a'))]++; | ||
} | ||
for(auto c:t){ | ||
cntVec[(int(c-'a'))]--; | ||
} | ||
for(auto cnt:cntVec){ | ||
if(cnt!=0){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
|
||
} | ||
}; | ||
``` | ||
- O(n) | ||
- 두번째 것과 마찬가지로 저장공간이 필요하나, O(n)으로 종료 가능 | ||
- 시간 복잡도를 최대한 최적화 하는 경우 | ||
|
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 @@ | ||
- https://leetcode.com/problems/validate-binary-search-tree/ |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.