From 7adadbe68d32559eb3c0081439243d3d0c5012f5 Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Sun, 16 Nov 2025 02:20:55 +0900 Subject: [PATCH 1/3] #218 & #230 & #239 solutions --- climbing-stairs/ys-han00.cpp | 18 ++++++++++++ product-of-array-except-self/ys-han00.cpp | 30 +++++++++++++++++++ valid-anagram/ys-han00.cpp | 36 +++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 climbing-stairs/ys-han00.cpp create mode 100644 product-of-array-except-self/ys-han00.cpp create mode 100644 valid-anagram/ys-han00.cpp diff --git a/climbing-stairs/ys-han00.cpp b/climbing-stairs/ys-han00.cpp new file mode 100644 index 0000000000..84d5b3abf8 --- /dev/null +++ b/climbing-stairs/ys-han00.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int climbStairs(int n) { + vector fibo(46, 0); + + fibo[1] = 1; + fibo[2] = 2; + + if(n <= 2) + return fibo[n]; + + for(int i = 3; i <= n; i++) + fibo[i] = fibo[i - 1] + fibo[i - 2]; + + return fibo[n]; + } +}; + diff --git a/product-of-array-except-self/ys-han00.cpp b/product-of-array-except-self/ys-han00.cpp new file mode 100644 index 0000000000..24ccfd8f4e --- /dev/null +++ b/product-of-array-except-self/ys-han00.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + vector productExceptSelf(vector& nums) { + int mul = 1, zero_cnt = 0; + + for(int i = 0; i < nums.size(); i++) { + if(nums[i] != 0) + mul *= nums[i]; + else + zero_cnt++; + } + + vector ans = vector(nums.size(), 0); + + if(zero_cnt == 1) { + for(int i = 0; i < nums.size(); i++) { + if(nums[i] == 0) { + ans[i] = mul; + break; + } + } + } else if(zero_cnt == 0) { + for(int i = 0; i < nums.size(); i++) + ans[i] = mul / nums[i]; + } + + return ans; + } +}; + diff --git a/valid-anagram/ys-han00.cpp b/valid-anagram/ys-han00.cpp new file mode 100644 index 0000000000..4944efd110 --- /dev/null +++ b/valid-anagram/ys-han00.cpp @@ -0,0 +1,36 @@ +// class Solution { +// public: +// bool isAnagram(string s, string t) { +// map counter; + +// for(int i = 0; i < s.size(); i++) +// counter[s[i] - 'a']++; +// for(int i = 0; i < t.size(); i++) +// counter[t[i] - 'a']--; + +// for(auto const& iter : counter) +// if(iter.second != 0) +// return false; + +// return true; +// } +// }; + +class Solution { +public: + bool isAnagram(string s, string t) { + vector counter(26, 0); + + for(int i = 0; i < s.size(); i++) + counter[s[i] - 'a']++; + for(int i = 0; i < t.size(); i++) + counter[t[i] - 'a']--; + + for(int i = 0; i < 26; i++) + if(counter[i] != 0) + return false; + + return true; + } +}; + From 6c0d93fd8f5a8485b486ef89c8b234197185286b Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Tue, 18 Nov 2025 23:55:17 +0900 Subject: [PATCH 2/3] update #239 solution --- product-of-array-except-self/ys-han00.cpp | 78 +++++++++++++++++------ 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/product-of-array-except-self/ys-han00.cpp b/product-of-array-except-self/ys-han00.cpp index 24ccfd8f4e..907e402f1b 100644 --- a/product-of-array-except-self/ys-han00.cpp +++ b/product-of-array-except-self/ys-han00.cpp @@ -1,27 +1,69 @@ +// class Solution { +// public: +// vector productExceptSelf(vector& nums) { +// int mul = 1, zero_cnt = 0; + +// for(int i = 0; i < nums.size(); i++) { +// if(nums[i] != 0) +// mul *= nums[i]; +// else +// zero_cnt++; +// } + +// vector ans = vector(nums.size(), 0); + +// if(zero_cnt == 1) { +// for(int i = 0; i < nums.size(); i++) { +// if(nums[i] == 0) { +// ans[i] = mul; +// break; +// } +// } +// } else if(zero_cnt == 0) { +// for(int i = 0; i < nums.size(); i++) +// ans[i] = mul / nums[i]; +// } + +// return ans; +// } +// }; + +// class Solution { +// public: +// vector productExceptSelf(vector& nums) { +// int mul = 1, zero_cnt = 0; +// vector prefix(nums.size(), 0), postfix(nums.size(), 0); + +// prefix[0] = 1; +// for(int i = 1; i < nums.size(); i++) +// prefix[i] = nums[i - 1] * prefix[i - 1]; + +// postfix[nums.size() - 1] = 1; +// for(int i = nums.size() - 2; i >= 0; i--) +// postfix[i] = nums[i + 1] * postfix[i + 1]; + +// vector ans = vector(nums.size(), 0); +// for(int i = 0; i < nums.size(); i++) +// ans[i] = postfix[i] * prefix[i]; + +// return ans; +// } +// }; + class Solution { public: vector productExceptSelf(vector& nums) { - int mul = 1, zero_cnt = 0; + vector ans(nums.size(), 1); + int before = 1, after = 1; - for(int i = 0; i < nums.size(); i++) { - if(nums[i] != 0) - mul *= nums[i]; - else - zero_cnt++; + for(int i = 0; i < nums.size() - 1; i++) { + before *= nums[i]; + ans[i + 1] *= before; } - vector ans = vector(nums.size(), 0); - - if(zero_cnt == 1) { - for(int i = 0; i < nums.size(); i++) { - if(nums[i] == 0) { - ans[i] = mul; - break; - } - } - } else if(zero_cnt == 0) { - for(int i = 0; i < nums.size(); i++) - ans[i] = mul / nums[i]; + for(int i = nums.size() - 1; i > 0; i--) { + after *= nums[i]; + ans[i - 1] *= after; } return ans; From 0a07a706ed85d5302c0f0a52a8205eb3bbef375d Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Wed, 19 Nov 2025 22:33:43 +0900 Subject: [PATCH 3/3] #241 & #251 solutions --- 3sum/ys-han00.cpp | 75 ++++++++++++++++++++++ validate-binary-search-tree/ys-han00.cpp | 79 ++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 3sum/ys-han00.cpp create mode 100644 validate-binary-search-tree/ys-han00.cpp diff --git a/3sum/ys-han00.cpp b/3sum/ys-han00.cpp new file mode 100644 index 0000000000..0ef0ee6d8e --- /dev/null +++ b/3sum/ys-han00.cpp @@ -0,0 +1,75 @@ +// class Solution { +// public: +// vector> threeSum(vector& nums) { +// set> ans; +// map counter; // {num : cnt} +// vector targets; +// int target, left, right; + +// for(int i = 0; i < nums.size(); i++) +// counter[nums[i]]++; + +// for(auto iter : counter) +// targets.push_back(iter.first); + +// for(int i = 0; i < targets.size(); i++) { +// target = -1 * targets[i]; + +// vector new_nums; +// counter[targets[i]]--; +// for(auto iter : counter) { +// for(int j = 0; j < iter.second; j++) +// new_nums.push_back(iter.first); +// } +// left = 0; right = new_nums.size() - 1; +// while(left < right) { +// int sum = new_nums[left] + new_nums[right]; +// if(sum == target && targets[i] <= new_nums[left] && new_nums[left] <= new_nums[right]) +// ans.insert(vector({targets[i], new_nums[left], new_nums[right]})); + +// if(sum < target) +// left++; +// else +// right--; +// } +// counter[targets[i]]++; +// } + +// return vector> (ans.begin(), ans.end()); +// } +// }; + +class Solution { +public: + vector> threeSum(vector& nums) { + vector> ans; + int left, right, sum; + + sort(nums.begin(), nums.end()); + + for(int i = 0; i < nums.size(); i++) { + if(i > 0 && nums[i] == nums[i - 1]) + continue; + + left = i + 1; right = nums.size() - 1; + while(left < right) { + sum = nums[i] + nums[left] + nums[right]; + if(sum == 0) { + ans.push_back(vector ({nums[i], nums[left], nums[right]})); + left++; right--; + while(left < right && nums[left] == nums[left - 1]) + left++; + while(left < right && nums[right] == nums[right + 1]) + right--; + } + else if(sum < 0) + left++; + else if(sum > 0) + right--; + } + } + + return ans; + } +}; + diff --git a/validate-binary-search-tree/ys-han00.cpp b/validate-binary-search-tree/ys-han00.cpp new file mode 100644 index 0000000000..ebc839ef34 --- /dev/null +++ b/validate-binary-search-tree/ys-han00.cpp @@ -0,0 +1,79 @@ +/** + * 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 isValidBST(TreeNode* root) { +// queue>>> que; +// int val, dir, num; + +// que.push({root, vector> ()}); +// while(!que.empty()) { +// TreeNode* node = que.front().first; +// vector> path = que.front().second; +// que.pop(); + +// val = node->val; + +// for(int i = 0; i < path.size(); i++) { +// num = path[i].first; +// dir = path[i].second; +// if(dir == 0 && num <= val) +// return false; +// if(dir == 1 && val <= num) +// return false; +// } + +// if(!node->left && !node->right) +// continue; + +// if(node->left) { +// path.push_back({val, 0}); +// que.push({node->left, path}); +// path.pop_back(); +// } + +// if(node->right) { +// path.push_back({val, 1}); +// que.push({node->right, path}); +// path.pop_back(); +// } +// } + +// return true; +// } +// }; + +class Solution { +public: + void inorder(TreeNode* root, vector& ordered) { + if(root==NULL) + return; + + inorder(root->left, ordered); + ordered.push_back(root->val); + inorder(root->right, ordered); + } + + bool isValidBST(TreeNode* root) { + vector ordered; + + inorder(root, ordered); + + for(int i = 1; i < ordered.size(); i++) + if(ordered[i - 1] >= ordered[i]) + return false; + + return true; + } +}; +