diff --git a/best-time-to-buy-and-sell-stock/njngwn.java b/best-time-to-buy-and-sell-stock/njngwn.java new file mode 100644 index 000000000..9e1e8f223 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/njngwn.java @@ -0,0 +1,16 @@ +// Time Complexity: O(n), n: prices.length +// Space Complexity: O(1), because 3 int-variables are declared -> O(1)+O(1)+O(1) +class Solution { + public int maxProfit(int[] prices) { + int buyPrice = prices[0]; + int sellPrice = 0; + int profit = 0; // store maxProfit + + for (int i = 1; i < prices.length; ++i) { + buyPrice = Math.min(buyPrice, prices[i]); + profit = Math.max(profit, prices[i] - buyPrice); + } + + return profit; + } +} diff --git a/group-anagrams/njngwn.java b/group-anagrams/njngwn.java new file mode 100644 index 000000000..a9e8acabf --- /dev/null +++ b/group-anagrams/njngwn.java @@ -0,0 +1,21 @@ +// Time Complexity: O(n), n: strs.length +// Space Complexity: O(m+n), m: strMap.size, n: anagramList.size +class Solution { + public List> groupAnagrams(String[] strs) { + HashMap> strMap = new HashMap>(); + + for (String str : strs) { + char[] charArr = str.toCharArray(); + Arrays.sort(charArr); + String key = String.valueOf(charArr); + + if (strMap.containsKey(key)) { + strMap.get(key).add(str); + } else { + strMap.put(key, new ArrayList(Arrays.asList(str))); + } + } + + return new ArrayList<>(strMap.values()); + } +} diff --git a/implement-trie-prefix-tree/njngwn.java b/implement-trie-prefix-tree/njngwn.java new file mode 100644 index 000000000..09ca4f605 --- /dev/null +++ b/implement-trie-prefix-tree/njngwn.java @@ -0,0 +1,65 @@ +// Time Complexity: O(n), n: word.length() +// Space Complexity: O(n), n: word.length() +class Trie { + private static class TrieNode { + HashMap trieNodeMap; + boolean isEnd; + + private TrieNode() { + this.trieNodeMap = new HashMap<>(); + this.isEnd = false; + } + } + + private TrieNode rootNode; + + public Trie() { + this.rootNode = new TrieNode(); + } + + public void insert(String word) { + TrieNode currentNode = this.rootNode; + + for (char ch : word.toCharArray()) { + if (!currentNode.trieNodeMap.containsKey(ch)) { + currentNode.trieNodeMap.put(ch, new TrieNode()); + } + currentNode = currentNode.trieNodeMap.get(ch); + } + currentNode.isEnd = true; + } + + public boolean search(String word) { + TrieNode currentNode = this.rootNode; + + for (char ch : word.toCharArray()) { + if (!currentNode.trieNodeMap.containsKey(ch)) { + return false; + } + currentNode = currentNode.trieNodeMap.get(ch); + } + + return currentNode.isEnd; + } + + public boolean startsWith(String prefix) { + TrieNode currentNode = this.rootNode; + + for (char ch : prefix.toCharArray()) { + if (!currentNode.trieNodeMap.containsKey(ch)) { + return false; + } + currentNode = currentNode.trieNodeMap.get(ch); + } + + return true; + } +} + +/** + * Your Trie object will be instantiated and called as such: + * Trie obj = new Trie(); + * obj.insert(word); + * boolean param_2 = obj.search(word); + * boolean param_3 = obj.startsWith(prefix); + */