-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path121.best-time-to-buy-and-sell-stock.py
70 lines (56 loc) · 1.7 KB
/
121.best-time-to-buy-and-sell-stock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#
# @lc app=leetcode id=121 lang=python3
#
# [121] Best Time to Buy and Sell Stock
#
# @lc code=start
import time
class Solution:
def maxProfit(self, prices: list[int]) -> int:
if not prices:
return 0
min = prices[0]
maxp = profit = 0
for i in range(1, len(prices)):
if prices[i] < min:
min = prices[i]
maxp = min
if prices[i] > maxp:
maxp = prices[i]
profit = max(maxp - min, profit)
return profit
def maxProfit2(self, prices: list[int]) -> int:
if len(prices) < 2:
return 0
profit = 0
for i in range(len(prices)-1):
for j in range(i+1, len(prices)):
profit = max(profit, prices[j] - prices[i])
return profit
def maxProfit3(self, prices: list[int]) -> int:
left = profit = 0
right = 1
while right < len(prices):
if prices[left] < prices[right]:
profit = max(prices[right] - prices[left], profit)
else:
left = right
right += 1
return profit
def maxProfit4(self, prices: list[int]) -> int:
if not prices:
return 0
results = [[0, -prices[0]]]
for i in range(1, len(prices)):
results.append([])
results[i].append( max(results[i-1][0], results[i-1][1] + prices[i]) )
results[i].append( max(results[i-1][1], results[i-1][0] - prices[i]) )
return results[-1][0]
# @lc code=end
time1 = time.time()
n = [7,1,5,3,6,4]
n = [1,2,3,4,5]
pro = Solution()
print(pro.maxProfit4(n))
time2 = time.time()
print(time2-time1)