diff --git a/Python/BestTimeToBuyAndSellStock.py b/Python/BestTimeToBuyAndSellStock.py new file mode 100644 index 0000000..d8ffeda --- /dev/null +++ b/Python/BestTimeToBuyAndSellStock.py @@ -0,0 +1,16 @@ +import math + +class Solution: + def maxProfit(self, prices: list[int]) -> int: + sellTwo = 0 + holdTwo = -math.inf + sellOne = 0 + holdOne = -math.inf + + for price in prices: + sellTwo = max(sellTwo, holdTwo + price) + holdTwo = max(holdTwo, sellOne - price) + sellOne = max(sellOne, holdOne + price) + holdOne = max(holdOne , -price) + + return sellTwo diff --git a/Python/PatchingArray.py b/Python/PatchingArray.py new file mode 100644 index 0000000..f53bbc1 --- /dev/null +++ b/Python/PatchingArray.py @@ -0,0 +1,14 @@ +class Solution: + def minPatches(self, nums: list[int], n: int) -> int: + patches = 0 + min_num = 1 + i = 0 + while (min_num <= n): + if i < len(nums) and nums[i] <= min_num: + min_num += nums[i] + i += 1 + else: + patches += 1 + min_num *= 2 + + return patches diff --git a/Python/ShortestPalindrome.py b/Python/ShortestPalindrome.py new file mode 100644 index 0000000..8ee61e0 --- /dev/null +++ b/Python/ShortestPalindrome.py @@ -0,0 +1,8 @@ +class Solution: + def shortestPalindrome(self, s: str) -> str: + t = s[::-1] + for i in range(len(s)): + if s.startswith(t[i:]): + return t[:i] + s + return t + s + \ No newline at end of file