diff --git a/Typescript/BestTimeToBuyAndSellStock.ts b/Typescript/BestTimeToBuyAndSellStock.ts new file mode 100644 index 0000000..f0b3bad --- /dev/null +++ b/Typescript/BestTimeToBuyAndSellStock.ts @@ -0,0 +1,16 @@ +function maxProfit(prices: number[]): number { + let sellTwo = 0; + let holdTwo = - Infinity; + let sellOne = 0; + let holdOne = -Infinity; + + for (let i = 0; i < prices.length; i++) { + const price = prices[i]; + + sellTwo = Math.max(sellTwo, holdTwo + price); + holdTwo = Math.max(holdTwo, sellOne - price); + sellOne = Math.max(sellOne, holdOne + price); + holdOne = Math.max(holdOne, -price); + } + return sellTwo; +}; \ No newline at end of file diff --git a/Typescript/PatchingArray.ts b/Typescript/PatchingArray.ts new file mode 100644 index 0000000..20f5ca5 --- /dev/null +++ b/Typescript/PatchingArray.ts @@ -0,0 +1,16 @@ +function minPatches(nums: number[], n: number): number { + let min_num = 1; + let patches = 0; + let i = 0; + + while (min_num <= n) { + if (i < nums.length && nums[i] <= min_num) { + min_num += nums[i]; + i++; + }else { + patches++; + min_num *= 2; + } + } + return patches; +}; \ No newline at end of file diff --git a/Typescript/ShortestPalindrome.ts b/Typescript/ShortestPalindrome.ts new file mode 100644 index 0000000..f76f581 --- /dev/null +++ b/Typescript/ShortestPalindrome.ts @@ -0,0 +1,11 @@ +function shortestPalindrome(s: string): string { + const t = s.split('').reverse().join(''); + + for (let i = 0; i < t.length; i++) { + if (s.startsWith(t.slice(i))) { + return t.slice(0, i) + s; + } + } + + return t + s; +}; \ No newline at end of file