-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path123_BestTimeToBuyAndSellStockIII.py
62 lines (53 loc) · 1.87 KB
/
123_BestTimeToBuyAndSellStockIII.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
def maxProfit(self, prices):
if not prices:
return 0
days_count = len(prices)
pre_profit = [0] * days_count
post_profit = [0] * days_count
# Get max profit when buy and sell Stock only once in pre ith day.
min_buy = prices[0]
for i in range(1, days_count):
min_buy = min(min_buy, prices[i])
pre_profit[i] = max(pre_profit[i - 1], prices[i] - min_buy)
# Get max profit when buy and sell Stock only once in post (n-i) days.
max_sell = prices[-1]
for j in range(days_count - 2, -1, -1):
max_sell = max(max_sell, prices[j])
post_profit[j] = max(post_profit[j + 1], max_sell - prices[j])
# Find the max profit when buy and sell Stock only twice.
# First in the pre kth day, and second in the post (n-k) days
max_profit = 0
for i in range(days_count):
max_profit = max(max_profit, pre_profit[i] + post_profit[i])
return max_profit
class Solution_2(object):
"""
Assume we only have 0 money at first, Then
sell_2: The maximum if we've just sold 2nd stock so far.
buy_2: The maximum if we've just buy 2nd stock so far.
sell_1: The maximum if we've just sold 1nd stock so far.
buy_1: The maximum if we've just buy 1st stock so far.
Refer to:
https://discuss.leetcode.com/topic/5934/is-it-best-solution-with-o-n-o-1
"""
def maxProfit(self, prices):
buy_1, buy_2 = -2**31, -2**31
sell_1, sell_2 = 0, 0
for p in prices:
sell_2 = max(sell_2, p + buy_2)
buy_2 = max(buy_2, sell_1 - p)
sell_1 = max(sell_1, p + buy_1)
buy_1 = max(buy_1, -p)
return sell_2
"""
[]
[1,2]
[1,3,5]
[2,8,3,9]
[2,8,3,9,1,2]
[2,8,3,9,1,9]
[6,5,4,3,2,1]
"""