-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
maximum-profit-from-trading-stocks.py
43 lines (36 loc) · 1.1 KB
/
maximum-profit-from-trading-stocks.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
# Time: O(n * b)
# Space: O(b)
import itertools
# dp, optimized from solution2
class Solution(object):
def maximumProfit(self, present, future, budget):
"""
:type present: List[int]
:type future: List[int]
:type budget: int
:rtype: int
"""
dp = [0]*(budget+1)
for i, (p, f) in enumerate(itertools.izip(present, future)):
if f-p < 0:
continue
for b in reversed(xrange(p, budget+1)):
dp[b] = max(dp[b], dp[b-p]+(f-p))
return dp[-1]
# Time: O(n * b)
# Space: O(b)
import itertools
# dp
class Solution2(object):
def maximumProfit(self, present, future, budget):
"""
:type present: List[int]
:type future: List[int]
:type budget: int
:rtype: int
"""
dp = [[0]*(budget+1) for _ in xrange(2)]
for i, (p, f) in enumerate(itertools.izip(present, future)):
for b in xrange(budget+1):
dp[(i+1)%2][b] = max(dp[i%2][b], (dp[i%2][b-p]+(f-p) if b-p >= 0 else 0))
return dp[len(present)%2][-1]