From a0cdd4d26ff90c59f863b2b47901cafc8221bb19 Mon Sep 17 00:00:00 2001 From: minji-go Date: Mon, 5 May 2025 17:37:18 +0900 Subject: [PATCH 01/12] feat: valid-parentheses --- valid-parentheses/minji-go.java | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 valid-parentheses/minji-go.java diff --git a/valid-parentheses/minji-go.java b/valid-parentheses/minji-go.java new file mode 100644 index 000000000..6efe81bcd --- /dev/null +++ b/valid-parentheses/minji-go.java @@ -0,0 +1,36 @@ +/** + * week06-1.valid-parentheses + *
  • Description: determine if the input string is valid
  • + *
  • Topics: String, Stack
  • + *
  • Time Complexity: O(N), Runtime 2ms
  • + *
  • Space Complexity: O(N), Memory 41.98MB
  • + */ + +class Solution { + public boolean isValid(String s) { + Deque stack = new ArrayDeque<>(); + for(char c : s.toCharArray()) { + if(isOpenBracket(c)){ + stack.push(c); + continue; + } + + if(stack.isEmpty() || isNoneMatchBracket(stack.pop(), c)) { + return false; + } + } + + return stack.isEmpty(); + } + + private boolean isOpenBracket(char open) { + return open == '(' || open == '{' || open == '['; + } + + private boolean isNoneMatchBracket(char open, char close) { + if(open == '(') return close != ')'; + if(open == '{') return close != '}'; + if(open == '[') return close != ']'; + return true; + } +} From 4128fed98ea77b1d9430aa0975d1f442b70eb3b5 Mon Sep 17 00:00:00 2001 From: minji-go Date: Mon, 5 May 2025 20:18:21 +0900 Subject: [PATCH 02/12] feat: container-with-most-water --- container-with-most-water/minji-go.java | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 container-with-most-water/minji-go.java diff --git a/container-with-most-water/minji-go.java b/container-with-most-water/minji-go.java new file mode 100644 index 000000000..7ef22a1db --- /dev/null +++ b/container-with-most-water/minji-go.java @@ -0,0 +1,28 @@ +/** + * week06-2.container-with-most-water + *
  • Description: Return the maximum amount of water a container can store
  • + *
  • Topics: Array, Two Pointers, Greedy
  • + *
  • Time Complexity: O(N), Runtime 5ms
  • + *
  • Space Complexity: O(1), Memory 57.42MB
  • + */ +class Solution { + public int maxArea(int[] height) { + int maxArea = 0; + int left = 0; + int right = height.length - 1; + + while (left < right) { + int width = right - left; + int minHeight = Math.min(height[left], height[right]); + maxArea = Math.max(maxArea, width * minHeight); + + if (height[left] < height[right]) { + left++; + } else { + right--; + } + } + + return maxArea; + } +} From ac6e9a9ba3da64a564f557d584ea2296d953d0b3 Mon Sep 17 00:00:00 2001 From: minji-go Date: Mon, 5 May 2025 21:39:23 +0900 Subject: [PATCH 03/12] feat: design-add-and-search-words-data-structure --- .../minji-go.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 design-add-and-search-words-data-structure/minji-go.java diff --git a/design-add-and-search-words-data-structure/minji-go.java b/design-add-and-search-words-data-structure/minji-go.java new file mode 100644 index 000000000..10ada7e44 --- /dev/null +++ b/design-add-and-search-words-data-structure/minji-go.java @@ -0,0 +1,69 @@ +/** + * week06-3.design-add-and-search-words-data-structure + *
  • Description: Design a data structure that supports adding new words and finding if a string matches any previously added string
  • + *
  • Topics: String, Depth-First Search, Design, Trie
  • + *
  • Time Complexity: O(N), Runtime 274ms
  • + *
  • Space Complexity: O(N), Memory 118.44MB
  • + */ +class WordDictionary { + private Trie dictionary; + + public WordDictionary() { + dictionary = new Trie(); + } + + public void addWord(String word) { + dictionary.add(word); + } + + public boolean search(String word) { + return dictionary.contains(word); + } + +} + +public class Trie { + private boolean isEnd; + private Map next; + + Trie() { + next = new HashMap<>(); + } + + public void add(String word) { + Trie trie = this; + for(char c : word.toCharArray()){ + trie = trie.next.computeIfAbsent(c, k -> new Trie()); + } + trie.isEnd = true; + } + + public boolean contains(String word) { + Trie trie = this; + for(int i=0; i Date: Tue, 6 May 2025 16:27:15 +0900 Subject: [PATCH 04/12] feat: longest-increasing-subsequence --- longest-increasing-subsequence/minji-go.java | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 longest-increasing-subsequence/minji-go.java diff --git a/longest-increasing-subsequence/minji-go.java b/longest-increasing-subsequence/minji-go.java new file mode 100644 index 000000000..040ee858d --- /dev/null +++ b/longest-increasing-subsequence/minji-go.java @@ -0,0 +1,28 @@ +/** + * week06-4.longest-increasing-subsequence + *
  • Description: return the length of the longest strictly increasing subsequence
  • + *
  • Topics: Array, Binary Search, Dynamic Programming
  • + *
  • Time Complexity: O(NLogN), Runtime 6ms
  • + *
  • Space Complexity: O(N), Memory 44.3MB
  • + */ +class Solution { + public int lengthOfLIS(int[] nums) { + List dp = new ArrayList<>(); + + for(int num : nums){ + int idx = Collections.binarySearch(dp, num); + + if(idx < 0) { + idx = -idx -1; + } + + if(idx == dp.size()) { + dp.add(num); + } else { + dp.set(idx, num); + } + } + + return dp.size(); + } +} From d10572084ef678f71a93e6b315553821362c6ff7 Mon Sep 17 00:00:00 2001 From: minji-go Date: Wed, 7 May 2025 16:52:17 +0900 Subject: [PATCH 05/12] feat: spiral-matrix --- spiral-matrix/minji-go.java | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 spiral-matrix/minji-go.java diff --git a/spiral-matrix/minji-go.java b/spiral-matrix/minji-go.java new file mode 100644 index 000000000..84290327e --- /dev/null +++ b/spiral-matrix/minji-go.java @@ -0,0 +1,39 @@ +/** + * week06-5.spiral-matrix + *
  • Description: return all elements of the matrix in spiral order
  • + *
  • Topics: Array, Matrix, Simulation
  • + *
  • Time Complexity: O(N*M), Runtime 0ms
  • + *
  • Space Complexity: O(1), Memory 41.95MB
  • + */ +class Solution { + public List spiralOrder(int[][] matrix) { + List answer = new ArrayList<>(); + int lr = 0; + int hr = matrix.length-1; + int lc = 0; + int hc = matrix[0].length-1; + + while (lr<=hr && lc<=hc) { + for(int c=lc; c<=hc && lr<=hr; c++) { + answer.add(matrix[lr][c]); + } + lr++; + + for(int r=lr; r<=hr && lc<=hc; r++) { + answer.add(matrix[r][hc]); + } + hc--; + + for(int c=hc; c>=lc && lr<=hr; c--){ + answer.add(matrix[hr][c]); + } + hr--; + + for(int r=hr; r>=lr && lc<=hc; r--){ + answer.add(matrix[r][lc]); + } + lc++; + } + return answer; + } +} From 97978b30aa1ff70dd14f5bfca54627f88105bc86 Mon Sep 17 00:00:00 2001 From: minji-go Date: Wed, 7 May 2025 17:36:51 +0900 Subject: [PATCH 06/12] feat: valid-palindrome --- valid-palindrome/minji-go.java | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/valid-palindrome/minji-go.java b/valid-palindrome/minji-go.java index 8c88ece8d..16598b940 100644 --- a/valid-palindrome/minji-go.java +++ b/valid-palindrome/minji-go.java @@ -1,22 +1,19 @@ -/* - Problem: https://leetcode.com/problems/valid-palindrome/ - Description: return true if it is a palindrome, alphanumeric characters(letters and numbers) reads the same forward and backward - Concept: Two Pointers, String - Time Complexity: O(n), Runtime: 10ms - Space Complexity: O(n), Memory: 58.6MB -*/ +/** + * week03-1.valid-palindrome + *
  • Description: return true if it is a palindrome
  • + *
  • Topics: Two Pointers, String
  • + *
  • Time Complexity: O(N), Runtime 13ms
  • + *
  • Space Complexity: O(N), Memory 45.55MB
  • + */ class Solution { public boolean isPalindrome(String s) { - String regex ="[^A-Za-z0-9]"; - String palindrome = s.replaceAll(regex,"").toLowerCase(); //replaceAll(), toLowerCase(): O(n) + String str = s.toLowerCase().replaceAll("[^0-9a-z]", ""); - boolean answer = true; - for(int i=0; i Date: Wed, 7 May 2025 18:07:09 +0900 Subject: [PATCH 07/12] feat: number-of-1-bits --- number-of-1-bits/minji-go.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 number-of-1-bits/minji-go.java diff --git a/number-of-1-bits/minji-go.java b/number-of-1-bits/minji-go.java new file mode 100644 index 000000000..0adb4a526 --- /dev/null +++ b/number-of-1-bits/minji-go.java @@ -0,0 +1,17 @@ +/** + * week03-2.number-of-1-bits + *
  • Description: returns the number of set bits in its binary representation
  • + *
  • Topics: Divide and Conquer, Bit Manipulation
  • + *
  • Time Complexity: O(logN), Runtime 0ms
  • + *
  • Space Complexity: O(1), Memory 41.95MB
  • + */ +class Solution { + public int hammingWeight(int n) { + int count = 0; + while(n != 0) { + n &= (n-1); + count++; + } + return count; + } +} From 5959fff68bf7e4ec5046c7776f68f83e22dc7563 Mon Sep 17 00:00:00 2001 From: minji-go Date: Thu, 8 May 2025 11:22:35 +0900 Subject: [PATCH 08/12] feat: combination-sum --- combination-sum/minji-go.java | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/combination-sum/minji-go.java b/combination-sum/minji-go.java index fac01b813..89b7c3e4a 100644 --- a/combination-sum/minji-go.java +++ b/combination-sum/minji-go.java @@ -1,31 +1,31 @@ -/* - Problem: https://leetcode.com/problems/combination-sum/ - Description: return a list of all unique combinations of candidates where the chosen numbers sum to target - Concept: Array, Backtracking - Time Complexity: O(Náµ€), Runtime 2ms - Space Complexity: O(T), Memory 44.88MB -*/ -class Solution { - public List> answer = new ArrayList<>(); +/** + * week03-3.combination-sum + *
  • Description: return a list of all unique combinations of candidates where the chosen numbers sum to target
  • + *
  • Topics: Array, Backtracking
  • + *
  • Time Complexity: O(K^T), Runtime 2ms
  • + *
  • Space Complexity: O(T), Memory 44.9MB
  • + */ +class Solution { public List> combinationSum(int[] candidates, int target) { + List> combinations = new ArrayList<>(); Arrays.sort(candidates); - findCombination(candidates, target, new ArrayList<>(), 0); - return answer; + dfs(candidates, target, 0, new ArrayList<>(), combinations); + return combinations; } - public void findCombination(int[] candidates, int target, List combination, int idx) { - if(target == 0) { - answer.add(new ArrayList<>(combination)); + public void dfs(int[] candidates, int target, int index, List combination, List> combinations) { + if (target == 0) { + combinations.add(new ArrayList<>(combination)); return; } - for(int i=idx; i target) break; + for (int i = index; i < candidates.length; i++) { + if (target - candidates[i] < 0) break; combination.add(candidates[i]); - findCombination(candidates, target-candidates[i], combination, i); - combination.remove(combination.size()-1); + dfs(candidates, target - candidates[i], i, combination, combinations); + combination.remove(combination.size() - 1); } } } From bff33940b74bd0f94ec3a79cae40b3f3de23d61b Mon Sep 17 00:00:00 2001 From: minji-go Date: Fri, 9 May 2025 21:07:23 +0900 Subject: [PATCH 09/12] feat: decode-ways --- decode-ways/minji-go.java | 48 ++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/decode-ways/minji-go.java b/decode-ways/minji-go.java index 33552c78f..6d90764bf 100644 --- a/decode-ways/minji-go.java +++ b/decode-ways/minji-go.java @@ -1,28 +1,34 @@ -/* - 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 -*/ +/** + * week03-5.decode-ways + *
  • Description: return the number of ways to decode it
  • + *
  • Topics: String, Dynamic Programming
  • + *
  • Time Complexity: O(N), Runtime 1ms
  • + *
  • Space Complexity: O(1), Memory 41.8MB
  • + */ 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]; + if (s.charAt(0) == '0') { + return 0; + } + + int last2 = 1; + int last1 = 1; + + for (int i = 1; i < s.length(); i++) { + int curr = 0; + if (s.charAt(i) != '0') { + curr += last1; + } - for(int i=2; i= 10 && num <= 26) { + curr += last2; + } + + last2 = last1; + last1 = curr; } - return dp[s.length()-1]; - } - public boolean decode(String s){ - int num = Integer.parseInt(s); - int numLength = (int) Math.log10(num) + 1; - if(num<0 || num>26 || numLength != s.length()) return false; - return true; + return last1; } } From 09c809556f1b6c052d0ef9a464ed474c146529ef Mon Sep 17 00:00:00 2001 From: minji-go Date: Sat, 10 May 2025 17:34:28 +0900 Subject: [PATCH 10/12] refactor: valid-palindrome space complexity --- valid-palindrome/minji-go.java | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/valid-palindrome/minji-go.java b/valid-palindrome/minji-go.java index 16598b940..4eb56e974 100644 --- a/valid-palindrome/minji-go.java +++ b/valid-palindrome/minji-go.java @@ -2,17 +2,41 @@ * week03-1.valid-palindrome *
  • Description: return true if it is a palindrome
  • *
  • Topics: Two Pointers, String
  • - *
  • Time Complexity: O(N), Runtime 13ms
  • - *
  • Space Complexity: O(N), Memory 45.55MB
  • + *
  • Time Complexity: O(N), Runtime 2ms
  • + *
  • Space Complexity: O(1), Memory 42.75MB
  • */ class Solution { public boolean isPalindrome(String s) { - String str = s.toLowerCase().replaceAll("[^0-9a-z]", ""); + int left = 0; + int right = s.length() - 1; - for (int i = 0; i < str.length() / 2; i++) { - if (str.charAt(i) != str.charAt(str.length() - 1 - i)) { + while (left < right) { + char charLeft = s.charAt(left); + char charRight = s.charAt(right); + if (isNotValidCharacter(charLeft)) { + left++; + continue; + } + if (isNotValidCharacter(charRight)) { + right--; + continue; + } + + if (Character.toLowerCase(charLeft) != Character.toLowerCase(charRight)) { return false; } + left++; + right--; + } + return true; + + } + + private boolean isNotValidCharacter(char c) { + if ((c >= '0' && c <= '9') + || (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z')) { + return false; } return true; } From 721b1d28176ce4ce693b44651cc5815a1429c20a Mon Sep 17 00:00:00 2001 From: minji-go Date: Sat, 10 May 2025 17:40:49 +0900 Subject: [PATCH 11/12] refactor: longest-increasing-subsequence naming --- longest-increasing-subsequence/minji-go.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/longest-increasing-subsequence/minji-go.java b/longest-increasing-subsequence/minji-go.java index 040ee858d..bbe224e2e 100644 --- a/longest-increasing-subsequence/minji-go.java +++ b/longest-increasing-subsequence/minji-go.java @@ -7,22 +7,22 @@ */ class Solution { public int lengthOfLIS(int[] nums) { - List dp = new ArrayList<>(); + List lisTails = new ArrayList<>(); for(int num : nums){ - int idx = Collections.binarySearch(dp, num); + int idx = Collections.binarySearch(lisTails, num); if(idx < 0) { idx = -idx -1; } - if(idx == dp.size()) { - dp.add(num); + if(idx == lisTails.size()) { + lisTails.add(num); } else { - dp.set(idx, num); + lisTails.set(idx, num); } } - return dp.size(); + return lisTails.size(); } } From 91ce5687492610147153f7a3f10b5dc48126ee4b Mon Sep 17 00:00:00 2001 From: minji-go Date: Sat, 10 May 2025 17:48:55 +0900 Subject: [PATCH 12/12] refactor: spiral-matrix redundant checks --- spiral-matrix/minji-go.java | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/spiral-matrix/minji-go.java b/spiral-matrix/minji-go.java index 84290327e..4e220f46d 100644 --- a/spiral-matrix/minji-go.java +++ b/spiral-matrix/minji-go.java @@ -9,30 +9,34 @@ class Solution { public List spiralOrder(int[][] matrix) { List answer = new ArrayList<>(); int lr = 0; - int hr = matrix.length-1; + int hr = matrix.length - 1; int lc = 0; - int hc = matrix[0].length-1; + int hc = matrix[0].length - 1; - while (lr<=hr && lc<=hc) { - for(int c=lc; c<=hc && lr<=hr; c++) { + while (lr <= hr && lc <= hc) { + for (int c = lc; c <= hc; c++) { answer.add(matrix[lr][c]); } lr++; - for(int r=lr; r<=hr && lc<=hc; r++) { + for (int r = lr; r <= hr; r++) { answer.add(matrix[r][hc]); } hc--; - for(int c=hc; c>=lc && lr<=hr; c--){ - answer.add(matrix[hr][c]); + if (lr <= hr) { + for (int c = hc; c >= lc; c--) { + answer.add(matrix[hr][c]); + } + hr--; } - hr--; - for(int r=hr; r>=lr && lc<=hc; r--){ - answer.add(matrix[r][lc]); + if (lc <= hc) { + for (int r = hr; r >= lr; r--) { + answer.add(matrix[r][lc]); + } + lc++; } - lc++; } return answer; }