-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path122.py
More file actions
23 lines (23 loc) · 655 Bytes
/
Copy path122.py
File metadata and controls
23 lines (23 loc) · 655 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
size = len(prices)
bought = False
profit = 0
price = 0
for i in range(0, size - 1):
if not bought:
if prices[i] < prices[i + 1]:
bought = True
price = prices[i]
else:
if prices[i] > prices[i + 1]:
bought = False
profit += prices[i] - price
price = 0
if bought:
profit += prices[i + 1] - price
return profit