Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 3sum/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> result;
for(int a = 0; a < nums.size()-2; a++) {
if (a > 0 && nums[a] == nums[a - 1]) continue;
int b = a + 1;
int c = nums.size() - 1;
while(b < c) {
if(nums[a] + nums[b] + nums[c] < 0) {
b++;
}
else if (nums[a] + nums[b] + nums[c] > 0) {
c--;
}
else {
result.push_back({nums[a], nums[b], nums[c]});
b++;
c--;
while (b < c && nums[b] == nums[b - 1]) b++;
while (b < c && nums[c] == nums[c + 1]) c--;
}
}
}

return result;
}
};
11 changes: 11 additions & 0 deletions climbing-stairs/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
int dp[45] = { 0 };
public:
int climbStairs(int n) {
dp[0] = 1;
if(n > 1) dp[1] = 2;
for(int i = 2; i < n; i++)
dp[i] = dp[i-1] + dp[i-2];
return dp[n-1];
}
};
20 changes: 20 additions & 0 deletions product-of-array-except-self/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> ans;
ans.resize(nums.size(), 1);
int p = 1;
int idx = nums.size()-1;
for(auto it = nums.rbegin(); it != nums.rend(); it++, idx--) {
ans[idx] *= p;
p *= *it; // 마지막 건 그냥 무시
}
p = 1;
idx = 0;
for(auto it = nums.begin(); it != nums.end(); it++, idx++) {
ans[idx] *= p;
p *= *it;
}
return ans;
}
};
14 changes: 14 additions & 0 deletions valid-anagram/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size() != t.size()) return false;
unordered_map<char, int> m1;
unordered_map<char, int> m2;
for(char c: s) m1[c]++;
for(char c: t) m2[c]++;
for(auto const& item: m1) {
if(m2[item.first] != item.second) return false;
}
return true;
}
};
30 changes: 30 additions & 0 deletions validate-binary-search-tree/dylan-jung.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요! 전체적으로 코드가 깔끔해서 이해하기 쉬웠어요.
다만 이 코드에서는 추가적으로 null 노드에 대한 base case를 체크해 주면 더 좋을 것 같아요.

bool isValidBST(TreeNode* root) {
    if (!root) return true;
    return dfs(root, -(1L << 32), 1L << 32);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

또 한 가지는, dfs 함수의 맨 처음에서

if (!(minVal < root->val && root->val < maxVal)) return false;

이 조건을 이미 검사하고 있기 때문에, 이 시점에서는 항상
minVal < root->val < maxVal 가 보장됩니다.

그래서 min((long)root->val, maxVal)는 실제로는 항상 root->val과 같고,
마찬가지로 right 쪽에서도 max((long)root->val, minVal)는 항상 root->val과 같아서

if (root->left) {
    isValid = isValid && dfs(root->left, minVal, root->val);
}
if (root->right) {
    isValid = isValid && dfs(root->right, root->val, maxVal);
}

처럼 수정해도 동일하게 동작을 하게 되는거 같아요.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요! 좋은 리뷰 주셔서 감사합니다 :)

  1. 제가 잘 안되는 부분 중 하나가 이렇게 엣지케이스 체킹하는 부분입니다. 놓친 부분 잘 체크해주셔서 감사합니다!

  2. 꼼꼼하게 살펴봐주셨네요 ㅎㅎㅎ 말씀해주신 부분이 맞습니다. 코드 고쳐서 푸시하겠습니다.

한 주 동안 고생 많으셨습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool dfs(TreeNode* root, long minVal, long maxVal) {
if(!(minVal < root->val && root->val < maxVal)) return false;
bool isValid = true;
if (root->left) {
isValid = isValid && dfs(root->left, minVal, root->val);
}
if (root->right) {
isValid = isValid && dfs(root->right, root->val, maxVal);
}
return isValid;
}

bool isValidBST(TreeNode* root) {
if(!root) return true;
return dfs(root, -(1l << 32), 1l << 32);
}
};