-
-
Notifications
You must be signed in to change notification settings - Fork 304
[dylan-jung] WEEK 02 solutions #2047
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
+104
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
861bfbc
valid anagram solution
dylan-jung a554bbd
climbing-stairs solutions
dylan-jung 5527f2b
product of array except self solution
dylan-jung 076b11c
3sum solution
dylan-jung 40761bd
validate-bin-search-tree
dylan-jung 59809f9
Merge branch 'main' into main
dylan-jung 1ae2729
Fix: validate bin search tree
dylan-jung 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,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; | ||
| } | ||
| }; |
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,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]; | ||
| } | ||
| }; |
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,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; | ||
| } | ||
| }; |
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,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; | ||
| } | ||
| }; |
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,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); | ||
| } | ||
| }; |
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.
안녕하세요! 전체적으로 코드가 깔끔해서 이해하기 쉬웠어요.
다만 이 코드에서는 추가적으로 null 노드에 대한 base case를 체크해 주면 더 좋을 것 같아요.
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.
또 한 가지는,
dfs함수의 맨 처음에서이 조건을 이미 검사하고 있기 때문에, 이 시점에서는 항상
minVal < root->val < maxVal 가 보장됩니다.
그래서 min((long)root->val, maxVal)는 실제로는 항상 root->val과 같고,
마찬가지로 right 쪽에서도 max((long)root->val, minVal)는 항상 root->val과 같아서
처럼 수정해도 동일하게 동작을 하게 되는거 같아요.
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.
안녕하세요! 좋은 리뷰 주셔서 감사합니다 :)
제가 잘 안되는 부분 중 하나가 이렇게 엣지케이스 체킹하는 부분입니다. 놓친 부분 잘 체크해주셔서 감사합니다!
꼼꼼하게 살펴봐주셨네요 ㅎㅎㅎ 말씀해주신 부분이 맞습니다. 코드 고쳐서 푸시하겠습니다.
한 주 동안 고생 많으셨습니다!