-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxProfits.py
43 lines (30 loc) · 1.16 KB
/
maxProfits.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
# Leetcode 121
class Solution:
def maxProfit(self, prices: List[int]) -> int:
prices_sorted = sorted(prices)
profits = []
for i in range(len(prices_sorted)):
j = len(prices_sorted)-1
while j >= i:
for a in range(len(prices)):
if prices[a] == prices_sorted[i]:
for b in range(a+1, len(prices)):
if prices[b] == prices_sorted[j]:
profits.append(prices[b] - prices[a])
j -= 1
if profits:
return max(profits)
else:
return 0
# OR
class SolutionTwo:
def maxProfit(self, prices: List[int]) -> int:
if prices == sorted(prices, reverse=True):
return 0
else:
profits = []
for i in range(len(prices)):
for j in range(i+1, len(prices)):
if prices[j] - prices[i] > 0:
profits.append(prices[j] - prices[i])
return max(profits)