From 2bf8fc01e8484e631f0e8ba9930c7bb1714665e7 Mon Sep 17 00:00:00 2001 From: "hyungmin.oh" Date: Mon, 7 Apr 2025 22:00:37 +0900 Subject: [PATCH 01/10] initial commit --- 3sum/haung921209.md | 0 climbing-stairs/haung921209.md | 0 product-of-array-except-self/haung921209.md | 0 valid-anagram/haung921209.md | 70 +++++++++++++++++++++ validate-binary-search-tree/haung921209.md | 0 5 files changed, 70 insertions(+) create mode 100644 3sum/haung921209.md create mode 100644 climbing-stairs/haung921209.md create mode 100644 product-of-array-except-self/haung921209.md create mode 100644 valid-anagram/haung921209.md create mode 100644 validate-binary-search-tree/haung921209.md diff --git a/3sum/haung921209.md b/3sum/haung921209.md new file mode 100644 index 000000000..e69de29bb diff --git a/climbing-stairs/haung921209.md b/climbing-stairs/haung921209.md new file mode 100644 index 000000000..e69de29bb diff --git a/product-of-array-except-self/haung921209.md b/product-of-array-except-self/haung921209.md new file mode 100644 index 000000000..e69de29bb diff --git a/valid-anagram/haung921209.md b/valid-anagram/haung921209.md new file mode 100644 index 000000000..6befa7a76 --- /dev/null +++ b/valid-anagram/haung921209.md @@ -0,0 +1,70 @@ + +```cpp +class Solution { +public: + bool isAnagram(string s, string t) { + if (s.length() != t.length()) + return false; + sort(s.begin(), s.end()); + sort(t.begin(), t.end()); + return s==t; + } +}; +``` + +- O(nlogn) +- std의 sort를 사용 + +``` +class Solution { +public: + bool isAnagram(string s, string t) { + if (s.length() != t.length()) + return false; + unordered_map dict; + for(auto c:s){ + dict[c]++; + } + for(auto c:t){ + if(dict[c]==0){ + return false; + } + dict[c]--; + } + return true; + + } +}; +``` + +- O(n)? => O(nlogn) +- map insert에서 O(logn)씩 소요됨 +- 위의 sort보다는 효율적일 수 있음 + +```cpp +class Solution { +public: + bool isAnagram(string s, string t) { + if (s.length() != t.length()) + return false; + vectorcntVec(26, 0); + for(auto c:s){ + cntVec[(int(c-'a'))]++; + } + for(auto c:t){ + cntVec[(int(c-'a'))]--; + } + for(auto cnt:cntVec){ + if(cnt!=0){ + return false; + } + } + return true; + + } +}; +``` +- O(n) +- 두번째 것과 마찬가지로 저장공간이 필요하나, O(n)으로 종료 가능 +- 시간 복잡도를 최대한 최적화 하는 경우 + diff --git a/validate-binary-search-tree/haung921209.md b/validate-binary-search-tree/haung921209.md new file mode 100644 index 000000000..e69de29bb From f67b65a95f020ef54cca96fd1198b2a91bf3c042 Mon Sep 17 00:00:00 2001 From: "hyungmin.oh" Date: Mon, 7 Apr 2025 22:02:52 +0900 Subject: [PATCH 02/10] =?UTF-8?q?climbing=20chairs=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- climbing-stairs/haung921209.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/climbing-stairs/haung921209.md b/climbing-stairs/haung921209.md index e69de29bb..b1af56dfa 100644 --- a/climbing-stairs/haung921209.md +++ b/climbing-stairs/haung921209.md @@ -0,0 +1,15 @@ +```cpp +class Solution { +public: + int climbStairs(int n) { + vector dp = {0, 1, 2}; + dp.resize(n+1); + for(int i=3;i<=n;i++){ + dp[i] = dp[i-1] + dp[i-2]; + } + return dp[n]; + } +}; +``` +- O(n) +- dp \ No newline at end of file From 222658eabc49092472cc9ee0cec88702f3d267b4 Mon Sep 17 00:00:00 2001 From: "hyungmin.oh" Date: Mon, 7 Apr 2025 22:18:11 +0900 Subject: [PATCH 03/10] product of array except self PR --- product-of-array-except-self/haung921209.md | 44 +++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/product-of-array-except-self/haung921209.md b/product-of-array-except-self/haung921209.md index e69de29bb..74865e028 100644 --- a/product-of-array-except-self/haung921209.md +++ b/product-of-array-except-self/haung921209.md @@ -0,0 +1,44 @@ +- URL : https://leetcode.com/problems/product-of-array-except-self/description/ +- points from constraints + - 2 <= nums.length <= 10^5 + - if not use O(n) algorithm, a TLE occurs + - -30 <= nums[i] <= 30 + - the production result can be negative + - do not use an unsigned type for the result object. + - The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer. + - There is no need to use a 64-bit (or larger) type for the result object. + - However, it is not guaranteed that the intermediate object will always be a 32-bit type. + + + +```cpp +class Solution { +public: + vector productExceptSelf(vector& nums) { + vector idxOfZero; + long long productRes = 1; + for(int i=0;i res(nums.size(), 0); + if(idxOfZero.size()>=2){ + return res; + }else if(idxOfZero.size()==1){ + res[idxOfZero[0]] = productRes; + return res; + } + + for(int i=0;i 64bit(by constraint #3) \ No newline at end of file From 0cc4f139b9beebc4a62cfef48d259118558d1a5e Mon Sep 17 00:00:00 2001 From: "hyungmin.oh" Date: Sat, 12 Apr 2025 08:09:18 +0900 Subject: [PATCH 04/10] commit --- .gitignore | 1 + 3sum/haung921209.md | 67 ++++++++++++++++++++++ validate-binary-search-tree/haung921209.md | 1 + 3 files changed, 69 insertions(+) diff --git a/.gitignore b/.gitignore index d2a36c905..9702f9a73 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .vscode/ .DS_Store .env +**/**-template.md \ No newline at end of file diff --git a/3sum/haung921209.md b/3sum/haung921209.md index e69de29bb..293ee4724 100644 --- a/3sum/haung921209.md +++ b/3sum/haung921209.md @@ -0,0 +1,67 @@ +```cpp +class Solution { +public: + vector> threeSum(vector& nums) { + sort(nums.begin(), nums.end()); + set> res; + for(int i=0;i0){ + r--; + }else{ + res.insert({nums[i], nums[l], nums[r]}); + l++; + r--; + } + } + } + + return vector>(res.begin(), res.end()); + } +}; +``` + +- set -> vector 사용 이유는 중복 제거를 위함 + +```cpp +class Solution { +public: + vector> threeSum(vector& nums) { + sort(nums.begin(), nums.end()); + set> res; + for(int i=0;i0){ + r--; + }else{ + res.insert({nums[i], nums[l], nums[r]}); + l++; + r--; + } + } + } + + return vector>(res.begin(), res.end()); + + } +}; +``` + +- `if(i != 0 && nums[i] == nums[i-1]) continue;` 를 통한 탐색 범위 줄이기 최적화 정도의 차이로 상 / 하위 갈리는 정도 +- 단순 2 pointer로 처리해도 무방 + + diff --git a/validate-binary-search-tree/haung921209.md b/validate-binary-search-tree/haung921209.md index e69de29bb..2caaae1c5 100644 --- a/validate-binary-search-tree/haung921209.md +++ b/validate-binary-search-tree/haung921209.md @@ -0,0 +1 @@ +- https://leetcode.com/problems/validate-binary-search-tree/ From e9805b04772444bb84455cf6563eb41e1eec5a78 Mon Sep 17 00:00:00 2001 From: "hyungmin.oh" Date: Sat, 12 Apr 2025 15:58:29 +0900 Subject: [PATCH 05/10] commit --- validate-binary-search-tree/haung921209.md | 74 +++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/validate-binary-search-tree/haung921209.md b/validate-binary-search-tree/haung921209.md index 2caaae1c5..889943afb 100644 --- a/validate-binary-search-tree/haung921209.md +++ b/validate-binary-search-tree/haung921209.md @@ -1 +1,73 @@ -- https://leetcode.com/problems/validate-binary-search-tree/ +# 연관 링크 +- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5) +- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C) + +# Problem +- 문제 링크 : https://leetcode.com/problems/validate-binary-search-tree/ +- 문제 이름 : validate-binary-search-tree +- 문제 번호 : 251 +- 난이도 : medium +- 카테고리 : binary search tree + +# 문제 설명 + +Given the root of a binary tree, determine if it is a valid binary search tree (BST). + +A valid BST is defined as follows: + +The left subtree of a node contains only nodes with keys less than the node's key. +The right subtree of a node contains only nodes with keys greater than the node's key. +Both the left and right subtrees must also be binary search trees. + +#### Constraints: + +- The number of nodes in the tree is in the range [1, 104]. +- 2^31 <= Node.val <= 2^31 - 1 + +# 아이디어 +- 각 노드 별 + + + +# 코드 (Solution) +```cpp + +class Solution { +public: + bool isValidBSTHelper(TreeNode* root, long min, long max){ + if (!root) { + return true; + } + + if (root->val <= min || root->val >= max) { + return false; + } + + return isValidBSTHelper(root->left, min, root->val) && + isValidBSTHelper(root->right, root->val, max); + } + + bool isValidBST(TreeNode* root) { + return isValidBSTHelper(root, LONG_MIN, LONG_MAX); + } +}; +``` + +## 코드 설명 + +- INT_MIN / INT_MAX가 Constraint의 경계값이지만, 해당 값을 이용할 경우 경계값 비교 시 번거로워지므로 LONG으로 비교하도록 처리 + + +# 최적화 포인트 (Optimality Discussion) +- 특이 사항 없음 + +# 🧪 테스트 & 엣지 케이스 + +## INT_MAX / INT_MIN으로 할 경우 +- root = [2147483647] 인 경우 엣지 케이스 + +# 📚 관련 지식 복습 + +# 🔁 회고 + + From 9fc7481390df9da4901ec252460e6408722f7264 Mon Sep 17 00:00:00 2001 From: "hyungmin.oh" Date: Sat, 12 Apr 2025 15:59:20 +0900 Subject: [PATCH 06/10] =?UTF-8?q?=EA=B0=9C=ED=96=89=20=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- product-of-array-except-self/haung921209.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/product-of-array-except-self/haung921209.md b/product-of-array-except-self/haung921209.md index 74865e028..42b7cf074 100644 --- a/product-of-array-except-self/haung921209.md +++ b/product-of-array-except-self/haung921209.md @@ -41,4 +41,7 @@ public: ``` - O(n) -- long long type for result object -> 64bit(by constraint #3) \ No newline at end of file +- long long type for result object -> 64bit(by constraint #3) + + + From f2f33c71cb4cf8ac3435560e4c94c8dc6b019fdb Mon Sep 17 00:00:00 2001 From: "hyungmin.oh" Date: Tue, 15 Apr 2025 21:15:22 +0900 Subject: [PATCH 07/10] =?UTF-8?q?=EA=B0=9C=ED=96=89=20=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9702f9a73..25a8e29ce 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,7 @@ .vscode/ .DS_Store .env -**/**-template.md \ No newline at end of file +**/**-template.md + + + From d7b599a186adcd268ceccabd506b14fe2fbcde46 Mon Sep 17 00:00:00 2001 From: "hyungmin.oh" Date: Tue, 15 Apr 2025 21:18:04 +0900 Subject: [PATCH 08/10] =?UTF-8?q?=EA=B0=9C=ED=96=89=20=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- climbing-stairs/haung921209.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/climbing-stairs/haung921209.md b/climbing-stairs/haung921209.md index b1af56dfa..dd472cf36 100644 --- a/climbing-stairs/haung921209.md +++ b/climbing-stairs/haung921209.md @@ -12,4 +12,7 @@ public: }; ``` - O(n) -- dp \ No newline at end of file +- dp + + + From a34e806ce1cfd1a2616688c4556c4ba399a55b60 Mon Sep 17 00:00:00 2001 From: Hyungmin Oh Date: Thu, 17 Apr 2025 09:56:18 +0900 Subject: [PATCH 09/10] Update .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 25a8e29ce..ef0f96746 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ .vscode/ .DS_Store .env -**/**-template.md From b42a0956a00df3fbeb6ea7e9b82a88c918b4acbb Mon Sep 17 00:00:00 2001 From: Hyungmin Oh Date: Thu, 17 Apr 2025 09:57:19 +0900 Subject: [PATCH 10/10] Update .gitignore --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index ef0f96746..d2a36c905 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,3 @@ .vscode/ .DS_Store .env - - -