Skip to content

Commit 04ed987

Browse files
authored
Merge pull request #1248 from haung921209/main
[haung921209] WEEK 02 solutions
2 parents 7958e43 + b42a095 commit 04ed987

File tree

5 files changed

+275
-0
lines changed

5 files changed

+275
-0
lines changed

3sum/haung921209.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
```cpp
2+
class Solution {
3+
public:
4+
vector<vector<int>> threeSum(vector<int>& nums) {
5+
sort(nums.begin(), nums.end());
6+
set<vector<int>> res;
7+
for(int i=0;i<nums.size();i++){
8+
int l = i+1, r = nums.size()-1;
9+
10+
while(l<r){
11+
int sum = nums[i]+nums[l]+nums[r];
12+
13+
if(sum<0){
14+
l++;
15+
}else if(sum>0){
16+
r--;
17+
}else{
18+
res.insert({nums[i], nums[l], nums[r]});
19+
l++;
20+
r--;
21+
}
22+
}
23+
}
24+
25+
return vector<vector<int>>(res.begin(), res.end());
26+
}
27+
};
28+
```
29+
30+
- set -> vector 사용 이유는 중복 제거를 위함
31+
32+
```cpp
33+
class Solution {
34+
public:
35+
vector<vector<int>> threeSum(vector<int>& nums) {
36+
sort(nums.begin(), nums.end());
37+
set<vector<int>> res;
38+
for(int i=0;i<nums.size();i++){
39+
if(i != 0 && nums[i] == nums[i-1]) continue;
40+
41+
int l = i+1, r = nums.size()-1;
42+
43+
while(l<r){
44+
int sum = nums[i]+nums[l]+nums[r];
45+
46+
if(sum<0){
47+
l++;
48+
}else if(sum>0){
49+
r--;
50+
}else{
51+
res.insert({nums[i], nums[l], nums[r]});
52+
l++;
53+
r--;
54+
}
55+
}
56+
}
57+
58+
return vector<vector<int>>(res.begin(), res.end());
59+
60+
}
61+
};
62+
```
63+
64+
- `if(i != 0 && nums[i] == nums[i-1]) continue;` 를 통한 탐색 범위 줄이기 최적화 정도의 차이로 상 / 하위 갈리는 정도
65+
- 단순 2 pointer로 처리해도 무방
66+
67+

climbing-stairs/haung921209.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
```cpp
2+
class Solution {
3+
public:
4+
int climbStairs(int n) {
5+
vector<int> dp = {0, 1, 2};
6+
dp.resize(n+1);
7+
for(int i=3;i<=n;i++){
8+
dp[i] = dp[i-1] + dp[i-2];
9+
}
10+
return dp[n];
11+
}
12+
};
13+
```
14+
- O(n)
15+
- dp
16+
17+
18+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
- URL : https://leetcode.com/problems/product-of-array-except-self/description/
2+
- points from constraints
3+
- 2 <= nums.length <= 10^5
4+
- if not use O(n) algorithm, a TLE occurs
5+
- -30 <= nums[i] <= 30
6+
- the production result can be negative
7+
- do not use an unsigned type for the result object.
8+
- The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer.
9+
- There is no need to use a 64-bit (or larger) type for the result object.
10+
- However, it is not guaranteed that the intermediate object will always be a 32-bit type.
11+
12+
13+
14+
```cpp
15+
class Solution {
16+
public:
17+
vector<int> productExceptSelf(vector<int>& nums) {
18+
vector<int> idxOfZero;
19+
long long productRes = 1;
20+
for(int i=0;i<nums.size();i++){
21+
if(nums[i]==0){
22+
idxOfZero.push_back(i);
23+
}else{
24+
productRes *=nums[i];
25+
}
26+
}
27+
vector<int> res(nums.size(), 0);
28+
if(idxOfZero.size()>=2){
29+
return res;
30+
}else if(idxOfZero.size()==1){
31+
res[idxOfZero[0]] = productRes;
32+
return res;
33+
}
34+
35+
for(int i=0;i<nums.size();i++){
36+
res[i] = (int)(productRes / nums[i]);
37+
}
38+
return res;
39+
}
40+
};
41+
```
42+
43+
- O(n)
44+
- long long type for result object -> 64bit(by constraint #3)
45+
46+
47+

valid-anagram/haung921209.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
```cpp
3+
class Solution {
4+
public:
5+
bool isAnagram(string s, string t) {
6+
if (s.length() != t.length())
7+
return false;
8+
sort(s.begin(), s.end());
9+
sort(t.begin(), t.end());
10+
return s==t;
11+
}
12+
};
13+
```
14+
15+
- O(nlogn)
16+
- std의 sort를 사용
17+
18+
```
19+
class Solution {
20+
public:
21+
bool isAnagram(string s, string t) {
22+
if (s.length() != t.length())
23+
return false;
24+
unordered_map<char, int> dict;
25+
for(auto c:s){
26+
dict[c]++;
27+
}
28+
for(auto c:t){
29+
if(dict[c]==0){
30+
return false;
31+
}
32+
dict[c]--;
33+
}
34+
return true;
35+
36+
}
37+
};
38+
```
39+
40+
- O(n)? => O(nlogn)
41+
- map insert에서 O(logn)씩 소요됨
42+
- 위의 sort보다는 효율적일 수 있음
43+
44+
```cpp
45+
class Solution {
46+
public:
47+
bool isAnagram(string s, string t) {
48+
if (s.length() != t.length())
49+
return false;
50+
vector<int>cntVec(26, 0);
51+
for(auto c:s){
52+
cntVec[(int(c-'a'))]++;
53+
}
54+
for(auto c:t){
55+
cntVec[(int(c-'a'))]--;
56+
}
57+
for(auto cnt:cntVec){
58+
if(cnt!=0){
59+
return false;
60+
}
61+
}
62+
return true;
63+
64+
}
65+
};
66+
```
67+
- O(n)
68+
- 두번째 것과 마찬가지로 저장공간이 필요하나, O(n)으로 종료 가능
69+
- 시간 복잡도를 최대한 최적화 하는 경우
70+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 연관 링크
2+
- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5)
3+
- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C)
4+
5+
# Problem
6+
- 문제 링크 : https://leetcode.com/problems/validate-binary-search-tree/
7+
- 문제 이름 : validate-binary-search-tree
8+
- 문제 번호 : 251
9+
- 난이도 : medium
10+
- 카테고리 : binary search tree
11+
12+
# 문제 설명
13+
14+
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
15+
16+
A valid BST is defined as follows:
17+
18+
The left subtree of a node contains only nodes with keys less than the node's key.
19+
The right subtree of a node contains only nodes with keys greater than the node's key.
20+
Both the left and right subtrees must also be binary search trees.
21+
22+
#### Constraints:
23+
24+
- The number of nodes in the tree is in the range [1, 104].
25+
- 2^31 <= Node.val <= 2^31 - 1
26+
27+
# 아이디어
28+
- 각 노드 별
29+
30+
31+
32+
# 코드 (Solution)
33+
```cpp
34+
35+
class Solution {
36+
public:
37+
bool isValidBSTHelper(TreeNode* root, long min, long max){
38+
if (!root) {
39+
return true;
40+
}
41+
42+
if (root->val <= min || root->val >= max) {
43+
return false;
44+
}
45+
46+
return isValidBSTHelper(root->left, min, root->val) &&
47+
isValidBSTHelper(root->right, root->val, max);
48+
}
49+
50+
bool isValidBST(TreeNode* root) {
51+
return isValidBSTHelper(root, LONG_MIN, LONG_MAX);
52+
}
53+
};
54+
```
55+
56+
## 코드 설명
57+
58+
- INT_MIN / INT_MAX가 Constraint의 경계값이지만, 해당 값을 이용할 경우 경계값 비교 시 번거로워지므로 LONG으로 비교하도록 처리
59+
60+
61+
# 최적화 포인트 (Optimality Discussion)
62+
- 특이 사항 없음
63+
64+
# 🧪 테스트 & 엣지 케이스
65+
66+
## INT_MAX / INT_MIN으로 할 경우
67+
- root = [2147483647] 인 경우 엣지 케이스
68+
69+
# 📚 관련 지식 복습
70+
71+
# 🔁 회고
72+
73+

0 commit comments

Comments
 (0)