diff --git a/best-time-to-buy-and-sell-stock/casentino.ts b/best-time-to-buy-and-sell-stock/casentino.ts new file mode 100644 index 0000000000..d6ef6361c9 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/casentino.ts @@ -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; +} diff --git a/group-anagrams/casentino.ts b/group-anagrams/casentino.ts new file mode 100644 index 0000000000..40e5e3b4fe --- /dev/null +++ b/group-anagrams/casentino.ts @@ -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()); +} diff --git a/implement-trie-prefix-tree/casentino.ts b/implement-trie-prefix-tree/casentino.ts new file mode 100644 index 0000000000..f17ebe70bd --- /dev/null +++ b/implement-trie-prefix-tree/casentino.ts @@ -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; + } +} diff --git a/word-break/casentino.ts b/word-break/casentino.ts new file mode 100644 index 0000000000..e6677e0700 --- /dev/null +++ b/word-break/casentino.ts @@ -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]; +}