Skip to content

Commit 81d5e7d

Browse files
authored
Merge pull request #1849 from njngwn/main
[njngwn] WEEK 05 solutions
2 parents 1eb94b9 + d1adf9a commit 81d5e7d

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time Complexity: O(n), n: prices.length
2+
// Space Complexity: O(1), because 3 int-variables are declared -> O(1)+O(1)+O(1)
3+
class Solution {
4+
public int maxProfit(int[] prices) {
5+
int buyPrice = prices[0];
6+
int sellPrice = 0;
7+
int profit = 0; // store maxProfit
8+
9+
for (int i = 1; i < prices.length; ++i) {
10+
buyPrice = Math.min(buyPrice, prices[i]);
11+
profit = Math.max(profit, prices[i] - buyPrice);
12+
}
13+
14+
return profit;
15+
}
16+
}

group-anagrams/njngwn.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time Complexity: O(n), n: strs.length
2+
// Space Complexity: O(m+n), m: strMap.size, n: anagramList.size
3+
class Solution {
4+
public List<List<String>> groupAnagrams(String[] strs) {
5+
HashMap<String, ArrayList<String>> strMap = new HashMap<String, ArrayList<String>>();
6+
7+
for (String str : strs) {
8+
char[] charArr = str.toCharArray();
9+
Arrays.sort(charArr);
10+
String key = String.valueOf(charArr);
11+
12+
if (strMap.containsKey(key)) {
13+
strMap.get(key).add(str);
14+
} else {
15+
strMap.put(key, new ArrayList<String>(Arrays.asList(str)));
16+
}
17+
}
18+
19+
return new ArrayList<>(strMap.values());
20+
}
21+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Time Complexity: O(n), n: word.length()
2+
// Space Complexity: O(n), n: word.length()
3+
class Trie {
4+
private static class TrieNode {
5+
HashMap<Character, TrieNode> trieNodeMap;
6+
boolean isEnd;
7+
8+
private TrieNode() {
9+
this.trieNodeMap = new HashMap<>();
10+
this.isEnd = false;
11+
}
12+
}
13+
14+
private TrieNode rootNode;
15+
16+
public Trie() {
17+
this.rootNode = new TrieNode();
18+
}
19+
20+
public void insert(String word) {
21+
TrieNode currentNode = this.rootNode;
22+
23+
for (char ch : word.toCharArray()) {
24+
if (!currentNode.trieNodeMap.containsKey(ch)) {
25+
currentNode.trieNodeMap.put(ch, new TrieNode());
26+
}
27+
currentNode = currentNode.trieNodeMap.get(ch);
28+
}
29+
currentNode.isEnd = true;
30+
}
31+
32+
public boolean search(String word) {
33+
TrieNode currentNode = this.rootNode;
34+
35+
for (char ch : word.toCharArray()) {
36+
if (!currentNode.trieNodeMap.containsKey(ch)) {
37+
return false;
38+
}
39+
currentNode = currentNode.trieNodeMap.get(ch);
40+
}
41+
42+
return currentNode.isEnd;
43+
}
44+
45+
public boolean startsWith(String prefix) {
46+
TrieNode currentNode = this.rootNode;
47+
48+
for (char ch : prefix.toCharArray()) {
49+
if (!currentNode.trieNodeMap.containsKey(ch)) {
50+
return false;
51+
}
52+
currentNode = currentNode.trieNodeMap.get(ch);
53+
}
54+
55+
return true;
56+
}
57+
}
58+
59+
/**
60+
* Your Trie object will be instantiated and called as such:
61+
* Trie obj = new Trie();
62+
* obj.insert(word);
63+
* boolean param_2 = obj.search(word);
64+
* boolean param_3 = obj.startsWith(prefix);
65+
*/

0 commit comments

Comments
 (0)