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
11 changes: 11 additions & 0 deletions best-time-to-buy-and-sell-stock/casentino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function maxProfit(prices: number[]): number {
const dp = new Array(prices.length).fill(0);
dp[0] = 0;
let maxProfit = 0;
for (let i = 1; i < prices.length; i++) {
dp[i] = Math.max(prices[i] - prices[i - 1], prices[i] - prices[i - 1] + dp[i - 1]);

maxProfit = Math.max(dp[i], maxProfit);
}
return maxProfit;
}
13 changes: 13 additions & 0 deletions group-anagrams/casentino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function groupAnagrams(strs: string[]): string[][] {
const hashMap = new Map();
for (let i = 0; i < strs.length; i++) {
const key = Array.from(strs[i]).sort().join("");

if (!hashMap.has(key)) {
hashMap.set(key, [strs[i]]);
} else {
hashMap.get(key).push(strs[i]);
}
}
return Array.from(hashMap.values());
}
55 changes: 55 additions & 0 deletions implement-trie-prefix-tree/casentino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
type ChildTrie = {
value?: string;
isEnd?: boolean;
childrens: ChildrenMap;
};
type ChildrenMap = {
[key: string]: ChildTrie;
};

class Trie {
childrens: ChildrenMap = {};
constructor() {}

insert(word: string): void {
if (word.length === 0) {
return;
}
let current: ChildTrie = {
childrens: this.childrens,
};
for (let i = 0; i < word.length; i++) {
if (current.childrens[word[i]] === undefined) {
current.childrens[word[i]] = { value: word[i], isEnd: false, childrens: {} };
}

current = current.childrens[word[i]];
}
current.isEnd = true;
}

search(word: string): boolean {
let current: ChildTrie = {
childrens: this.childrens,
};
for (let i = 0; i < word.length; i++) {
if (current.childrens[word[i]] === undefined) {
return false;
}

current = current.childrens[word[i]];
}
return current.isEnd ?? false;
}

startsWith(prefix: string): boolean {
let current = this.childrens;
for (let i = 0; i < prefix.length; i++) {
if (current[prefix[i]] === undefined) {
return false;
}
current = current[prefix[i]].childrens;
}
return true;
}
}
15 changes: 15 additions & 0 deletions word-break/casentino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function wordBreak(s: string, wordDict: string[]): boolean {
const dp = new Array(s.length + 1).fill(false);
dp[0] = true;
for (let i = 1; i <= s.length; i++) {
let str = "";
for (let j = 0; j < wordDict.length; j++) {
let start = i - wordDict[j].length;
if (start >= 0 && dp[start] && s.substring(start, i) === wordDict[j]) {
dp[i] = true;
break;
}
}
}
return dp[s.length];
}