diff --git a/3sum/dylan-jung.cpp b/3sum/dylan-jung.cpp new file mode 100644 index 000000000..d0fa834b9 --- /dev/null +++ b/3sum/dylan-jung.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + vector> threeSum(vector& nums) { + sort(nums.begin(), nums.end()); + vector> 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; + } +}; diff --git a/climbing-stairs/dylan-jung.cpp b/climbing-stairs/dylan-jung.cpp new file mode 100644 index 000000000..4262dc68d --- /dev/null +++ b/climbing-stairs/dylan-jung.cpp @@ -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]; + } +}; diff --git a/product-of-array-except-self/dylan-jung.cpp b/product-of-array-except-self/dylan-jung.cpp new file mode 100644 index 000000000..7c0cfc093 --- /dev/null +++ b/product-of-array-except-self/dylan-jung.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector productExceptSelf(vector& nums) { + vector 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; + } +}; diff --git a/valid-anagram/dylan-jung.cpp b/valid-anagram/dylan-jung.cpp new file mode 100644 index 000000000..3d5d74140 --- /dev/null +++ b/valid-anagram/dylan-jung.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + bool isAnagram(string s, string t) { + if(s.size() != t.size()) return false; + unordered_map m1; + unordered_map 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; + } +}; diff --git a/validate-binary-search-tree/dylan-jung.cpp b/validate-binary-search-tree/dylan-jung.cpp new file mode 100644 index 000000000..a454f9ca6 --- /dev/null +++ b/validate-binary-search-tree/dylan-jung.cpp @@ -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); + } +};