From 18635b9f13b6c81fba6644ff8c6e093a360779cb Mon Sep 17 00:00:00 2001 From: Vijay Raja Date: Wed, 27 Jan 2021 15:42:37 -0500 Subject: [PATCH 1/5] Create BTBS-I.py --- BTBS-I.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 BTBS-I.py diff --git a/BTBS-I.py b/BTBS-I.py new file mode 100644 index 0000000..c049900 --- /dev/null +++ b/BTBS-I.py @@ -0,0 +1,14 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + profit = 0 + lowSoFar = 1000000 + for i in range(len(prices)): + + lowSoFar = min(lowSoFar, prices[i]) + profit = max(profit, prices[i]-lowSoFar) + + return profit + + +Time: O(N) +Space: O(1) From d76e26b2e505a1fb881e14682a9cd491499948c8 Mon Sep 17 00:00:00 2001 From: Vijay Raja Date: Wed, 27 Jan 2021 15:42:58 -0500 Subject: [PATCH 2/5] Rename BTBS-I.py to Best Time to Buy and Sell Stock-I.py --- BTBS-I.py => Best Time to Buy and Sell Stock-I.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename BTBS-I.py => Best Time to Buy and Sell Stock-I.py (100%) diff --git a/BTBS-I.py b/Best Time to Buy and Sell Stock-I.py similarity index 100% rename from BTBS-I.py rename to Best Time to Buy and Sell Stock-I.py From f5ba4b42e68991afd8e16b86210bd186df843767 Mon Sep 17 00:00:00 2001 From: Vijay Raja Date: Wed, 27 Jan 2021 15:43:50 -0500 Subject: [PATCH 3/5] Create Best Time to Buy and Sell Stock-II.py --- Best Time to Buy and Sell Stock-II.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Best Time to Buy and Sell Stock-II.py diff --git a/Best Time to Buy and Sell Stock-II.py b/Best Time to Buy and Sell Stock-II.py new file mode 100644 index 0000000..4252569 --- /dev/null +++ b/Best Time to Buy and Sell Stock-II.py @@ -0,0 +1,10 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + sum = 0 + for i in range(1,len(prices)): + + sum += max(0, prices[i]-prices[i-1]) + + return sum +Time: O(N) +Space: O(1) From 74de550688db7be18f67aed71e483e2c2ec537ca Mon Sep 17 00:00:00 2001 From: Vijay Raja Date: Wed, 27 Jan 2021 15:47:15 -0500 Subject: [PATCH 4/5] Create Best Time to Buy and Sell Stock-III.py --- Best Time to Buy and Sell Stock-III.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Best Time to Buy and Sell Stock-III.py diff --git a/Best Time to Buy and Sell Stock-III.py b/Best Time to Buy and Sell Stock-III.py new file mode 100644 index 0000000..7308f28 --- /dev/null +++ b/Best Time to Buy and Sell Stock-III.py @@ -0,0 +1,22 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + firstCost = float("inf") + secondCost = float("inf") + + firstProfit = 0 + secondProfit = 0 + + + for cost in prices: + + firstCost = min(firstCost, cost) + + firstProfit = max(firstProfit, cost-firstCost) + + secondCost = min(secondCost, cost-firstProfit) + + secondProfit = max(secondProfit, cost-secondCost) + + return secondProfit +Time: O(N) +Space: O(1) From 76dbc695f697fa28174bba6d9b718d6930baedd7 Mon Sep 17 00:00:00 2001 From: Vijay Raja Date: Wed, 27 Jan 2021 16:35:29 -0500 Subject: [PATCH 5/5] Create Best Time to Buy and Sell Stock IV.py --- Best Time to Buy and Sell Stock IV.py | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Best Time to Buy and Sell Stock IV.py diff --git a/Best Time to Buy and Sell Stock IV.py b/Best Time to Buy and Sell Stock IV.py new file mode 100644 index 0000000..ed23ad2 --- /dev/null +++ b/Best Time to Buy and Sell Stock IV.py @@ -0,0 +1,28 @@ +class Solution: + def maxProfit(self, k: int, prices: List[int]) -> int: + + + if not prices: + return 0 + + + if k>=len(prices)//2: + ans = 0 + for i in range(1,len(prices)): + ans += max(0,prices[i]-prices[i-1]) + return ans + + + cost = [float("inf") for i in range(k+1)] + profit = [0 for i in range((k+1))] + + for price in prices: + for i in range(1,(k+1)): + cost[i] = min(cost[i], price-profit[i-1]) + profit[i] = max(profit[i], price-cost[i]) + + + return profit[-1] + +Time: NK +Space: O(K)