diff --git a/best-time-to-buy-and-sell-stock/minji-go.java b/best-time-to-buy-and-sell-stock/minji-go.java new file mode 100644 index 000000000..19b58272d --- /dev/null +++ b/best-time-to-buy-and-sell-stock/minji-go.java @@ -0,0 +1,18 @@ +/** + * week05-1.best-time-to-buy-and-sell-stock + *
  • Description: Return the maximum profit you can achieve from this transaction
  • + *
  • Topics: Array, Dynamic Programming
  • + *
  • Time Complexity: O(N), Runtime 2ms
  • + *
  • Space Complexity: O(1), Memory 61.62MB
  • + */ +class Solution { + public int maxProfit(int[] prices) { + int min = prices[0]; + int profit = 0; + for(int i=1; iweek05-2.group-anagrams + *
  • Description: Given an array of strings strs, group the anagrams together
  • + *
  • Topics: Array, Hash Table, String, Sorting
  • + *
  • Time Complexity: O(N*KLogK), Runtime 6ms
  • + *
  • Space Complexity: O(N*K), Memory 47.82MB
  • + */ +class Solution { + public List> groupAnagrams(String[] strs) { + Map> mapOfAnagrams = new HashMap<>(); + + for(String str : strs) { + char[] chars = str.toCharArray(); + Arrays.sort(chars); + String key = new String(chars); + + List anagrams = mapOfAnagrams.computeIfAbsent(key, k -> new ArrayList<>()); + anagrams.add(str); + } + + return new ArrayList<>(mapOfAnagrams.values()); + } +} diff --git a/implement-trie-prefix-tree/minji-go.java b/implement-trie-prefix-tree/minji-go.java new file mode 100644 index 000000000..3960d0385 --- /dev/null +++ b/implement-trie-prefix-tree/minji-go.java @@ -0,0 +1,44 @@ +/** + * week05-3.implement-trie-prefix-tree + *
  • Description: Implement the Trie class
  • + *
  • Topics: Hash Table, String, Design, Trie
  • + *
  • Time Complexity: O(N*K), Runtime 40ms
  • + *
  • Space Complexity: O(N*K), Memory 56.07MB
  • + */ +class Trie { + private boolean end; + private final Map children; + + public Trie() { + children = new HashMap<>(); + } + + public void insert(String word) { + Trie node = this; + for(Character c : word.toCharArray()) { + node = node.children.computeIfAbsent(c, k -> new Trie()); + } + node.end = true; + } + + public boolean search(String word) { + Trie node = this; + for(Character c : word.toCharArray()) { + node = node.children.get(c); + if(node == null) { + return false; + } + } + return node.end; + } + + public boolean startsWith(String prefix) { + Trie node = this; + for(Character c : prefix.toCharArray()) { + node = node.children.get(c); + if(node == null) { + return false; + } + } + return node != null; + } diff --git a/validate-binary-search-tree/minji-go.java b/validate-binary-search-tree/minji-go.java new file mode 100644 index 000000000..ffd00e6cb --- /dev/null +++ b/validate-binary-search-tree/minji-go.java @@ -0,0 +1,28 @@ +/** + * week02-5.validate-binary-search-tree + *
  • Description: determine if it is a valid binary search tree (BST).
  • + *
  • Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • + *
  • Time Complexity: O(N), Runtime 0ms
  • + *
  • Space Complexity: O(N), Memory 43.25MB
  • + */ +class Solution { + public boolean isValidBST(TreeNode root) { + return isValid(Integer.MIN_VALUE, Integer.MAX_VALUE, root); + } + + public boolean isValid(long min, long max, TreeNode node) { + if(node == null) { + return true; + } + + if (node.val < min || node.val > max) { + return false; + } + + return isValid(min, (long) node.val - 1, node.left) + && isValid((long) node.val + 1, max, node.right); + } + +} + + diff --git a/word-break/minji-go.java b/word-break/minji-go.java new file mode 100644 index 000000000..97c9884ae --- /dev/null +++ b/word-break/minji-go.java @@ -0,0 +1,53 @@ +/** + * week05-4.word-break + *
  • Description: return true if s can be segmented into a space-separated sequence of one or more dictionary words.
  • + *
  • Topics: Array, Hash Table, String, Dynamic Programming, Trie, Memoization
  • + *
  • Time Complexity: O(N³), Runtime 7ms
  • + *
  • Space Complexity: O(N+W), Memory 44.59MB
  • + */ +class Solution { + public boolean wordBreak(String s, List wordDict) { + Trie trie = new Trie(); + for (String str : wordDict) { + trie.insert(str); + } + + List dp = new ArrayList<>(); + dp.add(-1); + for (int i = 0; i < s.length(); i++) { + for (int j = dp.size() - 1; j >= 0; j--) { + int startIndex = dp.get(j); + if (trie.search(s.substring(startIndex + 1, i + 1))) { + dp.add(i); + break; + } + } + } + + return dp.contains(s.length() - 1); + } + + class Trie { + private boolean end; + private final Map children = new HashMap<>(); + + public void insert(String word) { + Trie node = this; + for (Character c : word.toCharArray()) { + node = node.children.computeIfAbsent(c, k -> new Trie()); + } + node.end = true; + } + + public boolean search(String word) { + Trie node = this; + for (Character c : word.toCharArray()) { + node = node.children.get(c); + if (node == null) { + return false; + } + } + return node.end; + } + } +}