Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions best-time-to-buy-and-sell-stock/njngwn.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
21 changes: 21 additions & 0 deletions group-anagrams/njngwn.java
Original file line number Diff line number Diff line change
@@ -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<List<String>> groupAnagrams(String[] strs) {
HashMap<String, ArrayList<String>> strMap = new HashMap<String, ArrayList<String>>();

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<String>(Arrays.asList(str)));
}
}

return new ArrayList<>(strMap.values());
}
}
65 changes: 65 additions & 0 deletions implement-trie-prefix-tree/njngwn.java
Original file line number Diff line number Diff line change
@@ -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<Character, TrieNode> 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);
*/