From 2a6b73a84232c4b140daf7b6ece970c7c6d066f6 Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Fri, 30 Jun 2023 22:29:51 -0700 Subject: [PATCH] Complete, Beats 11% runtime, beats 42% memory --- .../122BestTimetoBuyandSellStock2.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Top150Interview/122BestTimetoBuyandSellStock2.py b/Top150Interview/122BestTimetoBuyandSellStock2.py index 655e173..4831c50 100644 --- a/Top150Interview/122BestTimetoBuyandSellStock2.py +++ b/Top150Interview/122BestTimetoBuyandSellStock2.py @@ -3,17 +3,17 @@ def maxProfit(prices): :type prices: List[int] :rtype: int """ - lowest = min(prices) - if lowest == prices[len(prices)-1]: - print('no profit') - return 0 + profit = 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 + # print(i) + if i+1 == len(prices): + break + if prices[i] < prices[i+1]: + # print(i) + # print('hit') + profit += prices[i+1] - prices[i] + # print(profit) + return profit maxProfit([7,1,5,3,6,4]) # maxProfit([7,6,4,3,1]) \ No newline at end of file