Skip to content

Commit bc39741

Browse files
authored
[casentino] WEEK 05 solutions
[casentino] WEEK 05 solutions
2 parents b6c8fd4 + 2c831f6 commit bc39741

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function maxProfit(prices: number[]): number {
2+
const dp = new Array(prices.length).fill(0);
3+
dp[0] = 0;
4+
let maxProfit = 0;
5+
for (let i = 1; i < prices.length; i++) {
6+
dp[i] = Math.max(prices[i] - prices[i - 1], prices[i] - prices[i - 1] + dp[i - 1]);
7+
8+
maxProfit = Math.max(dp[i], maxProfit);
9+
}
10+
return maxProfit;
11+
}

group-anagrams/casentino.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function groupAnagrams(strs: string[]): string[][] {
2+
const hashMap = new Map();
3+
for (let i = 0; i < strs.length; i++) {
4+
const key = Array.from(strs[i]).sort().join("");
5+
6+
if (!hashMap.has(key)) {
7+
hashMap.set(key, [strs[i]]);
8+
} else {
9+
hashMap.get(key).push(strs[i]);
10+
}
11+
}
12+
return Array.from(hashMap.values());
13+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
type ChildTrie = {
2+
value?: string;
3+
isEnd?: boolean;
4+
childrens: ChildrenMap;
5+
};
6+
type ChildrenMap = {
7+
[key: string]: ChildTrie;
8+
};
9+
10+
class Trie {
11+
childrens: ChildrenMap = {};
12+
constructor() {}
13+
14+
insert(word: string): void {
15+
if (word.length === 0) {
16+
return;
17+
}
18+
let current: ChildTrie = {
19+
childrens: this.childrens,
20+
};
21+
for (let i = 0; i < word.length; i++) {
22+
if (current.childrens[word[i]] === undefined) {
23+
current.childrens[word[i]] = { value: word[i], isEnd: false, childrens: {} };
24+
}
25+
26+
current = current.childrens[word[i]];
27+
}
28+
current.isEnd = true;
29+
}
30+
31+
search(word: string): boolean {
32+
let current: ChildTrie = {
33+
childrens: this.childrens,
34+
};
35+
for (let i = 0; i < word.length; i++) {
36+
if (current.childrens[word[i]] === undefined) {
37+
return false;
38+
}
39+
40+
current = current.childrens[word[i]];
41+
}
42+
return current.isEnd ?? false;
43+
}
44+
45+
startsWith(prefix: string): boolean {
46+
let current = this.childrens;
47+
for (let i = 0; i < prefix.length; i++) {
48+
if (current[prefix[i]] === undefined) {
49+
return false;
50+
}
51+
current = current[prefix[i]].childrens;
52+
}
53+
return true;
54+
}
55+
}

word-break/casentino.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function wordBreak(s: string, wordDict: string[]): boolean {
2+
const dp = new Array(s.length + 1).fill(false);
3+
dp[0] = true;
4+
for (let i = 1; i <= s.length; i++) {
5+
let str = "";
6+
for (let j = 0; j < wordDict.length; j++) {
7+
let start = i - wordDict[j].length;
8+
if (start >= 0 && dp[start] && s.substring(start, i) === wordDict[j]) {
9+
dp[i] = true;
10+
break;
11+
}
12+
}
13+
}
14+
return dp[s.length];
15+
}

0 commit comments

Comments
 (0)