From dbc1ffca8b1888f918881a82f5324a17a96d9090 Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Tue, 27 Jun 2023 00:37:47 -0700 Subject: [PATCH] clears 150/215 test cases, need to fix edge case --- .../121BestTimetoBuyandSellStock.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Top150Interview/121BestTimetoBuyandSellStock.py b/Top150Interview/121BestTimetoBuyandSellStock.py index e69de29..c68b4cf 100644 --- a/Top150Interview/121BestTimetoBuyandSellStock.py +++ b/Top150Interview/121BestTimetoBuyandSellStock.py @@ -0,0 +1,19 @@ +def maxProfit(prices): + """ + :type prices: List[int] + :rtype: int + """ + lowest = min(prices) + if lowest == prices[len(prices)-1]: + print('no profit') + return 0 + for i in range(len(prices)): + if lowest == prices[i]: + k = i+1 + # print(f'k is {k}') + profit = max(prices[k:]) - lowest + print(profit) + return profit + +# maxProfit([7,1,5,3,6,4]) +maxProfit([7,6,4,3,1]) \ No newline at end of file