From 5439322bf452ae70b9b64fe99e7f078d7bcfa0ad Mon Sep 17 00:00:00 2001 From: Minji Ko Date: Tue, 17 Dec 2024 18:54:52 +0900 Subject: [PATCH 1/9] feat: climbing stairs --- climbing-stairs/minji-go.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 climbing-stairs/minji-go.java diff --git a/climbing-stairs/minji-go.java b/climbing-stairs/minji-go.java new file mode 100644 index 000000000..a71ded513 --- /dev/null +++ b/climbing-stairs/minji-go.java @@ -0,0 +1,18 @@ +/* + Problem: https://leetcode.com/problems/climbing-stairs/ + Description: how many distinct ways can you climb to the top, if you can either climb 1 or 2 steps + Concept: Dynamic Programming, Memoization, Recursion, Math, Array, Iterator, Combinatorics ... + Time Complexity: O(n), Runtime: 0ms + Space Complexity: O(1), Memory: 40.51MB +*/ +class Solution { + public int climbStairs(int n) { + int step1=1, step2=2; + for(int i=3; i Date: Tue, 17 Dec 2024 18:55:07 +0900 Subject: [PATCH 2/9] feat: valid anagram --- valid-anagram/minji-go.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 valid-anagram/minji-go.java diff --git a/valid-anagram/minji-go.java b/valid-anagram/minji-go.java new file mode 100644 index 000000000..b51300731 --- /dev/null +++ b/valid-anagram/minji-go.java @@ -0,0 +1,25 @@ +/* + Problem: https://leetcode.com/problems/valid-anagram/ + Description: return true if one string is an anagram of the other, one formed by rearranging the letters of the other + Concept:String, Hash Table, Sorting, Array, Counting, String Matching, Ordered Map, Ordered Set, Hash Function ... + Time Complexity: O(n), Runtime: 27ms + Space Complexity: O(n), Memory: 43.11MB +*/ +import java.util.HashMap; +import java.util.Map; + +class Solution { + public boolean isAnagram(String s, String t) { + if(s.length() != t.length()) return false; + + Map count = new HashMap<>(); + for(int i=0; i Date: Fri, 20 Dec 2024 00:40:57 +0900 Subject: [PATCH 3/9] feat: construct binary tree from preorder and inorder traversal --- .../minji-go.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/minji-go.java diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/minji-go.java b/construct-binary-tree-from-preorder-and-inorder-traversal/minji-go.java new file mode 100644 index 000000000..1f7f3a769 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/minji-go.java @@ -0,0 +1,67 @@ +/* + Problem: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ + Description: Given two integer arrays preorder and inorder, construct and return the binary tree. + Concept: Array, Hash Table, Divide and Conquer, Tree, Binary Tree + Time Complexity: O(NM), Runtime 2ms + Space Complexity: O(N), Memory 45.02MB +*/ +import java.util.HashMap; +import java.util.Map; + +class Solution { + public TreeNode buildTree(int[] preorder, int[] inorder) { + boolean isLeft = true; + Map parents = new HashMap<>(); + TreeNode rootNode = null, parentNode = null; + + for (int pidx=0, iidx=0; pidx Date: Fri, 20 Dec 2024 00:41:06 +0900 Subject: [PATCH 4/9] feat: decode ways --- decode-ways/minjigo.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 decode-ways/minjigo.java diff --git a/decode-ways/minjigo.java b/decode-ways/minjigo.java new file mode 100644 index 000000000..33552c78f --- /dev/null +++ b/decode-ways/minjigo.java @@ -0,0 +1,28 @@ +/* + Problem: https://leetcode.com/problems/decode-ways/ + Description: Given a string s containing only digits, return the number of ways to decode it + Concept: String, Dynamic Programming + Time Complexity: O(N), Runtime 1ms + Space Complexity: O(N), Memory 42.12MB +*/ +class Solution { + public int numDecodings(String s) { + int[] dp = new int[s.length()]; + if(decode(s.substring(0, 1))) dp[0]=1; + if(s.length()>1 && decode(s.substring(1, 2))) dp[1]+=dp[0]; + if(s.length()>1 && decode(s.substring(0, 2))) dp[1]+=dp[0]; + + for(int i=2; i26 || numLength != s.length()) return false; + return true; + } +} From 1892c98e7ae71c4dfd89892f2422965533dcb49d Mon Sep 17 00:00:00 2001 From: minji-go Date: Fri, 20 Dec 2024 02:44:21 +0900 Subject: [PATCH 5/9] feat: 3sum --- 3sum/minji-go.java | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 3sum/minji-go.java diff --git a/3sum/minji-go.java b/3sum/minji-go.java new file mode 100644 index 000000000..42928e008 --- /dev/null +++ b/3sum/minji-go.java @@ -0,0 +1,37 @@ +/* + Problem: https://leetcode.com/problems/3sum/ + Description: return all the triplets (i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0) + Concept: Array, Two Pointers, Sorting + Time Complexity: O(N²), Runtime 1107ms + Space Complexity: O(N), Memory 54.20MB +*/ +class Solution { + public List> threeSum(int[] nums) { + Map number = new HashMap<>(); + for(int i=0; i> set = new HashSet<>(); + List> triplets = new ArrayList<>(); + List lastTriplet = null; + for(int i=0; i-twoSum) continue; + + int count = number.getOrDefault(-twoSum,0); + if(nums[i]==-twoSum) count--; + if(nums[j]==-twoSum) count--; + if(count<=0) continue; + + List triplet = List.of(nums[i], nums[j], -twoSum); + int setSize = set.size(); + set.add(triplet); + if(setSize != set.size()) triplets.add(triplet); + } + } + return triplets; + } +} From 79d0f15cef3122759ea9d94758b0a5077ee795ee Mon Sep 17 00:00:00 2001 From: minji-go Date: Fri, 20 Dec 2024 02:45:33 +0900 Subject: [PATCH 6/9] =?UTF-8?q?refactor:=20=ED=8C=8C=EC=9D=BC=EB=AA=85=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- decode-ways/{minjigo.java => minji-go.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename decode-ways/{minjigo.java => minji-go.java} (100%) diff --git a/decode-ways/minjigo.java b/decode-ways/minji-go.java similarity index 100% rename from decode-ways/minjigo.java rename to decode-ways/minji-go.java From 895e6b2b96864a1af9c8ff828a840a64fecb98e2 Mon Sep 17 00:00:00 2001 From: minji-go Date: Fri, 20 Dec 2024 03:02:47 +0900 Subject: [PATCH 7/9] refactor: climbing stairs - early return --- climbing-stairs/minji-go.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/climbing-stairs/minji-go.java b/climbing-stairs/minji-go.java index a71ded513..b4856f493 100644 --- a/climbing-stairs/minji-go.java +++ b/climbing-stairs/minji-go.java @@ -3,16 +3,19 @@ Description: how many distinct ways can you climb to the top, if you can either climb 1 or 2 steps Concept: Dynamic Programming, Memoization, Recursion, Math, Array, Iterator, Combinatorics ... Time Complexity: O(n), Runtime: 0ms - Space Complexity: O(1), Memory: 40.51MB + Space Complexity: O(1), Memory: 40.63MB */ class Solution { public int climbStairs(int n) { - int step1=1, step2=2; - for(int i=3; i Date: Fri, 20 Dec 2024 03:07:14 +0900 Subject: [PATCH 8/9] refactor: valid anagram - map.values() --- valid-anagram/minji-go.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/valid-anagram/minji-go.java b/valid-anagram/minji-go.java index b51300731..035d8d130 100644 --- a/valid-anagram/minji-go.java +++ b/valid-anagram/minji-go.java @@ -3,7 +3,7 @@ Description: return true if one string is an anagram of the other, one formed by rearranging the letters of the other Concept:String, Hash Table, Sorting, Array, Counting, String Matching, Ordered Map, Ordered Set, Hash Function ... Time Complexity: O(n), Runtime: 27ms - Space Complexity: O(n), Memory: 43.11MB + Space Complexity: O(n), Memory: 43.05MB */ import java.util.HashMap; import java.util.Map; @@ -12,13 +12,13 @@ class Solution { public boolean isAnagram(String s, String t) { if(s.length() != t.length()) return false; - Map count = new HashMap<>(); + Map charCount = new HashMap<>(); for(int i=0; i Date: Sat, 21 Dec 2024 17:10:03 +0900 Subject: [PATCH 9/9] refactor: 3sum - remove set --- 3sum/minji-go.java | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/3sum/minji-go.java b/3sum/minji-go.java index 42928e008..86930fa49 100644 --- a/3sum/minji-go.java +++ b/3sum/minji-go.java @@ -2,8 +2,8 @@ Problem: https://leetcode.com/problems/3sum/ Description: return all the triplets (i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0) Concept: Array, Two Pointers, Sorting - Time Complexity: O(N²), Runtime 1107ms - Space Complexity: O(N), Memory 54.20MB + Time Complexity: O(N²), Runtime 70ms + Space Complexity: O(N), Memory 51.63MB */ class Solution { public List> threeSum(int[] nums) { @@ -17,19 +17,23 @@ public List> threeSum(int[] nums) { List> triplets = new ArrayList<>(); List lastTriplet = null; for(int i=0; i0 && nums[i]==nums[i-1]) continue; + for(int j=i+1; j-twoSum) continue; + if(j>i+1 && nums[j]==nums[j-1]) continue; + + int target = -(nums[i]+nums[j]); + if(nums[j]>target) continue; - int count = number.getOrDefault(-twoSum,0); - if(nums[i]==-twoSum) count--; - if(nums[j]==-twoSum) count--; + int count = number.getOrDefault(target,0); + if(nums[i]==target) count--; + if(nums[j]==target) count--; if(count<=0) continue; - List triplet = List.of(nums[i], nums[j], -twoSum); - int setSize = set.size(); - set.add(triplet); - if(setSize != set.size()) triplets.add(triplet); + List triplet = List.of(nums[i], nums[j], target); + if(triplet.equals(lastTriplet)) continue; + lastTriplet = triplet; + triplets.add(triplet); } } return triplets;