Skip to content

Commit 3c1d4d5

Browse files
authored
Merge pull request #2047 from dylan-jung/main
[dylan-jung] WEEK 02 solutions
2 parents 7fcb44c + 1ae2729 commit 3c1d4d5

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

3sum/dylan-jung.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> threeSum(vector<int>& nums) {
4+
sort(nums.begin(), nums.end());
5+
vector<vector<int>> result;
6+
for(int a = 0; a < nums.size()-2; a++) {
7+
if (a > 0 && nums[a] == nums[a - 1]) continue;
8+
int b = a + 1;
9+
int c = nums.size() - 1;
10+
while(b < c) {
11+
if(nums[a] + nums[b] + nums[c] < 0) {
12+
b++;
13+
}
14+
else if (nums[a] + nums[b] + nums[c] > 0) {
15+
c--;
16+
}
17+
else {
18+
result.push_back({nums[a], nums[b], nums[c]});
19+
b++;
20+
c--;
21+
while (b < c && nums[b] == nums[b - 1]) b++;
22+
while (b < c && nums[c] == nums[c + 1]) c--;
23+
}
24+
}
25+
}
26+
27+
return result;
28+
}
29+
};

climbing-stairs/dylan-jung.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
int dp[45] = { 0 };
3+
public:
4+
int climbStairs(int n) {
5+
dp[0] = 1;
6+
if(n > 1) dp[1] = 2;
7+
for(int i = 2; i < n; i++)
8+
dp[i] = dp[i-1] + dp[i-2];
9+
return dp[n-1];
10+
}
11+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
vector<int> productExceptSelf(vector<int>& nums) {
4+
vector<int> ans;
5+
ans.resize(nums.size(), 1);
6+
int p = 1;
7+
int idx = nums.size()-1;
8+
for(auto it = nums.rbegin(); it != nums.rend(); it++, idx--) {
9+
ans[idx] *= p;
10+
p *= *it; // 마지막 건 그냥 무시
11+
}
12+
p = 1;
13+
idx = 0;
14+
for(auto it = nums.begin(); it != nums.end(); it++, idx++) {
15+
ans[idx] *= p;
16+
p *= *it;
17+
}
18+
return ans;
19+
}
20+
};

valid-anagram/dylan-jung.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
bool isAnagram(string s, string t) {
4+
if(s.size() != t.size()) return false;
5+
unordered_map<char, int> m1;
6+
unordered_map<char, int> m2;
7+
for(char c: s) m1[c]++;
8+
for(char c: t) m2[c]++;
9+
for(auto const& item: m1) {
10+
if(m2[item.first] != item.second) return false;
11+
}
12+
return true;
13+
}
14+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool dfs(TreeNode* root, long minVal, long maxVal) {
15+
if(!(minVal < root->val && root->val < maxVal)) return false;
16+
bool isValid = true;
17+
if (root->left) {
18+
isValid = isValid && dfs(root->left, minVal, root->val);
19+
}
20+
if (root->right) {
21+
isValid = isValid && dfs(root->right, root->val, maxVal);
22+
}
23+
return isValid;
24+
}
25+
26+
bool isValidBST(TreeNode* root) {
27+
if(!root) return true;
28+
return dfs(root, -(1l << 32), 1l << 32);
29+
}
30+
};

0 commit comments

Comments
 (0)