From 81a5acb83d1763966fec8ffa960a64e1377cb396 Mon Sep 17 00:00:00 2001 From: casentino Date: Fri, 12 Dec 2025 23:16:26 +0900 Subject: [PATCH 1/4] Best Time to Buy and Sell Stock --- best-time-to-buy-and-sell-stock/casentino.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/casentino.ts 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; +} From e09e5877184c2a252eab47fb863964ab9146f3b5 Mon Sep 17 00:00:00 2001 From: casentino Date: Fri, 12 Dec 2025 23:17:08 +0900 Subject: [PATCH 2/4] Group Anagram --- group-anagrams/casentino.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 group-anagrams/casentino.ts 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()); +} From 04a6a59fa77051197fe5aa6f5b7b5f14c985f1cd Mon Sep 17 00:00:00 2001 From: casentino Date: Fri, 12 Dec 2025 23:18:25 +0900 Subject: [PATCH 3/4] Implement Trie (Prefix Tree) --- implement-trie-prefix-tree/casentino.ts | 55 +++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 implement-trie-prefix-tree/casentino.ts 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; + } +} From 2c831f667115bcb773085f365851508b09d15892 Mon Sep 17 00:00:00 2001 From: casentino Date: Fri, 12 Dec 2025 23:19:05 +0900 Subject: [PATCH 4/4] Word Break --- word-break/casentino.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 word-break/casentino.ts 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]; +}