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); } } } 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; + } +} 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; } } 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; iweek06-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 lisTails = new ArrayList<>(); + + for(int num : nums){ + int idx = Collections.binarySearch(lisTails, num); + + if(idx < 0) { + idx = -idx -1; + } + + if(idx == lisTails.size()) { + lisTails.add(num); + } else { + lisTails.set(idx, num); + } + } + + return lisTails.size(); + } +} 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; + } +} diff --git a/spiral-matrix/minji-go.java b/spiral-matrix/minji-go.java new file mode 100644 index 000000000..4e220f46d --- /dev/null +++ b/spiral-matrix/minji-go.java @@ -0,0 +1,43 @@ +/** + * 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; c++) { + answer.add(matrix[lr][c]); + } + lr++; + + for (int r = lr; r <= hr; r++) { + answer.add(matrix[r][hc]); + } + hc--; + + if (lr <= hr) { + for (int c = hc; c >= lc; c--) { + answer.add(matrix[hr][c]); + } + hr--; + } + + if (lc <= hc) { + for (int r = hr; r >= lr; r--) { + answer.add(matrix[r][lc]); + } + lc++; + } + } + return answer; + } +} diff --git a/valid-palindrome/minji-go.java b/valid-palindrome/minji-go.java index 8c88ece8d..4eb56e974 100644 --- a/valid-palindrome/minji-go.java +++ b/valid-palindrome/minji-go.java @@ -1,22 +1,43 @@ -/* - 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 2ms
  • + *
  • Space Complexity: O(1), Memory 42.75MB
  • + */ class Solution { public boolean isPalindrome(String s) { - String regex ="[^A-Za-z0-9]"; - String palindrome = s.replaceAll(regex,"").toLowerCase(); //replaceAll(), toLowerCase(): O(n) + int left = 0; + int right = s.length() - 1; - boolean answer = true; - for(int i=0; i= '0' && c <= '9') + || (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z')) { + return false; } - return answer; + return true; } } 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; + } +}