Skip to content

Commit 6f58f05

Browse files
authored
Merge pull request #1381 from Jeehay28/main
[Jeehay28] Week05 Solutions
2 parents 0cb5510 + 1f5688a commit 6f58f05

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time Complexity: O(n), where n is the length of the prices array
2+
// Space Complexity: O(1)
3+
4+
function maxProfit(prices: number[]): number {
5+
// input: an array prices, prices[i] = the stock price of ith day
6+
// output: the maximum profit || 0
7+
8+
// prices = [7, 1, 5, 3, 6, 4]
9+
// buy = 1, sell = 6, profit = 6 -1 = 5
10+
11+
let minBuy = prices[0];
12+
let maxProfit = 0;
13+
14+
for (let i = 1; i < prices.length; i++) {
15+
minBuy = Math.min(prices[i], minBuy);
16+
maxProfit = Math.max(prices[i] - minBuy, maxProfit);
17+
}
18+
19+
return maxProfit;
20+
}
21+

group-anagrams/Jeehay28.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Approach 2
2+
// Time Complexity: O(n * L), where n = number of strings in strs, L = maximum length of a string in strs
3+
// Space Complexity: O(n * L)
4+
5+
function groupAnagrams(strs: string[]): string[][] {
6+
7+
let sorted: { [key: string]: string[] } = {}
8+
9+
for (const str of strs) {
10+
11+
const arr = Array.from({ length: 26 }, () => 0)
12+
13+
for (const ch of str) {
14+
arr[ch.charCodeAt(0) - "a".charCodeAt(0)] += 1
15+
}
16+
17+
// const key = arr.join(""); // This can lead to key collisions.
18+
const key = arr.join("#"); // ✅
19+
20+
if (!sorted[key]) {
21+
sorted[key] = []
22+
}
23+
24+
sorted[key].push(str)
25+
}
26+
27+
return Object.values(sorted);
28+
};
29+
30+
31+
// Approach 1
32+
// Time Complexity: O(n * LlogL), where n = number of strings, L = average length of the strings
33+
// Space Complexity: O(n * L)
34+
35+
// function groupAnagrams(strs: string[]): string[][] {
36+
// // input: an array of strings, strs
37+
// // output: the anagrams
38+
39+
// // eat -> e, a, t -> a, e, t
40+
// // tea -> t, e, a -> a, e, t
41+
// // ate -> a, t, e -> a, e, t
42+
43+
// let map = new Map<string, string[]>();
44+
// let result: string[][] = [];
45+
46+
// for (const str of strs) {
47+
// const temp = str.split("").sort().join("");
48+
49+
// if (map.has(temp)) {
50+
// map.set(temp, [...map.get(temp)!, str]);
51+
// } else {
52+
// map.set(temp, [str]);
53+
// }
54+
// }
55+
56+
// for (const el of map.values()) {
57+
// result.push(el);
58+
// }
59+
60+
// return result;
61+
// }
62+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Time Complexity: O(n)
2+
3+
class TrieNode {
4+
children: { [key: string]: TrieNode };
5+
ending: boolean;
6+
7+
constructor(ending = false) {
8+
this.children = {};
9+
this.ending = ending;
10+
}
11+
}
12+
13+
class Trie {
14+
root: TrieNode;
15+
16+
constructor() {
17+
this.root = new TrieNode();
18+
}
19+
20+
insert(word: string): void {
21+
let node = this.root;
22+
23+
for (const ch of word) {
24+
if (!(ch in node.children)) {
25+
node.children[ch] = new TrieNode();
26+
}
27+
node = node.children[ch];
28+
}
29+
node.ending = true;
30+
}
31+
32+
search(word: string): boolean {
33+
let node = this.root;
34+
35+
for (const ch of word) {
36+
if (!(ch in node.children)) {
37+
return false;
38+
}
39+
node = node.children[ch];
40+
}
41+
return node.ending;
42+
}
43+
44+
startsWith(prefix: string): boolean {
45+
let node = this.root;
46+
47+
for (const ch of prefix) {
48+
if (!(ch in node.children)) {
49+
return false;
50+
}
51+
52+
node = node.children[ch];
53+
}
54+
return true;
55+
}
56+
}
57+
58+
/**
59+
* Your Trie object will be instantiated and called as such:
60+
* var obj = new Trie()
61+
* obj.insert(word)
62+
* var param_2 = obj.search(word)
63+
* var param_3 = obj.startsWith(prefix)
64+
*/

0 commit comments

Comments
 (0)