Skip to content

Commit f3534db

Browse files
author
Chris Wu
committed
no message
1 parent f96764a commit f3534db

6 files changed

+138
-3
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution(object):
2+
def maxProfit(self, prices):
3+
if not prices or len(prices)<=1: return 0
4+
N = len(prices)
5+
6+
buy = [-prices[0], max(-prices[1], -prices[0])]
7+
sell = [0, max(prices[1]+buy[0], 0)]
8+
9+
for i in xrange(2, N):
10+
buy.append(max(sell[i-2]-prices[i], buy[i-1]))
11+
sell.append(max(prices[i]+buy[i-1], sell[i-1]))
12+
13+
return max(buy[-1], sell[-1], 0)
14+
15+
"""
16+
Three rules:
17+
1. Need to buy before sell.
18+
2. After sell, next day need to rest (cannot buy).
19+
3. When buying, we spend money, which is a negative profit.
20+
21+
There are two possible end state. buy or sell. So we need to consider both.
22+
Only considering prices 0~i, buy[i] stores the max profit that the last action is "buy".
23+
Only considering prices 0~i, sell[i] stores the max profit that the last action is "sell".
24+
25+
Let's sort this out.
26+
When i==0:
27+
buy: -prices[0]
28+
sell: 0, since we cannot sell at i==0.
29+
30+
When i==1:
31+
buy: max(-prices[1], -prices[0])
32+
Now, we must not have sell yet. So no need to consider it.
33+
If we buy at i==1, the profit will be `-prices[1]`. But we also had the option not to buy. buy[0] is the max profit if we don't buy at i==1.
34+
Again, we are considering, when the end state is "buy", what is the max profit?
35+
Thus, `max(-prices[1], buy[0])`.
36+
37+
sell: max(prices[1]+buy[0], 0)
38+
If we sell at i==1, the profit will be `prices[1]+buy[0]`. But we also had the option not to sell. 0 is the max profit if we don't sell at i==1.
39+
Again, we are considering, when the end state is "sell", what is the max profit?
40+
Thus, `max(prices[1]+buy[0], sell[0])`
41+
42+
When i>=2:
43+
44+
45+
46+
47+
"""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
To accomplish 0(N) solution:
3+
For each i and price in the iteration, we need to keep track of the loest price before i.
4+
Also, update the max_profit in the iteration.
5+
"""
6+
class Solution(object):
7+
def maxProfit(self, prices):
8+
if not prices: return 0
9+
max_profit = 0
10+
lowest = prices[0]
11+
12+
for i in xrange(len(prices)):
13+
if i==0: continue
14+
max_profit = max(max_profit, prices[i]-lowest)
15+
lowest = min(lowest, prices[i])
16+
return max_profit

problems/house-robber-ii.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,23 @@ def rob(self, nums):
3939
else:
4040
K[i] = K[i-1]+nums[i]
4141
v2 = K[-1]
42-
return max(v1, v2)
42+
return max(v1, v2)
43+
44+
#2020/11/15
45+
class Solution(object):
46+
def rob(self, nums):
47+
if not nums: return 0
48+
if len(nums)==0 or len(nums)==1: return max(nums)
49+
N = len(nums)
50+
51+
v1 = nums[0]
52+
v2 = nums[0]
53+
for i in xrange(2, N-1):
54+
v1, v2 = max(nums[i]+v2, v1), v1
55+
56+
w1 = nums[1]
57+
w2 = 0
58+
for i in xrange(2, N):
59+
w1, w2 = max(nums[i]+w2, w1), w1
60+
61+
return max(v1, w1)

problems/house-robber.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,31 @@ def rob(self, nums):
2626
K[i] = max(K[i-2]+nums[i], K[i-1])
2727
else: #[2]
2828
K[i] = K[i-1]+nums[i]
29-
return K[-1]
29+
return K[-1]
30+
31+
#2020/11/14
32+
class Solution(object):
33+
def rob(self, nums):
34+
def helper(i):
35+
if i>=len(nums): return 0
36+
if i in history: return history[i]
37+
history[i] = max(nums[i]+helper(i+2), helper(i+1))
38+
return history[i]
39+
40+
history = {}
41+
return helper(0)
42+
43+
#2020/11/14
44+
class Solution(object):
45+
def rob(self, nums):
46+
if not nums: return 0
47+
if len(nums)==0 or len(nums)==1: return max(nums)
48+
49+
last1 = max(nums[0], nums[1])
50+
last2 = nums[0]
51+
52+
for i in xrange(len(nums)):
53+
if i==0 or i==1: continue
54+
last2, last1 = last1, max(nums[i]+last2, last1)
55+
56+
return last1

problems/maximum-subarray.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,17 @@ def maxSubArray(self, nums):
1919
maxCurrent = [nums[0]] #[1]
2020
for i in xrange(1, len(nums)):
2121
maxCurrent.append(max(nums[i], nums[i]+maxCurrent[-1])) #[0]
22-
return max(maxCurrent) #[2]
22+
return max(maxCurrent) #[2]
23+
24+
# 2020/11/14
25+
class Solution(object):
26+
def maxSubArray(self, nums):
27+
ans = nums[0]
28+
last_max = nums[0]
29+
30+
for i in xrange(len(nums)):
31+
if i==0: continue
32+
last_max = max(nums[i], nums[i]+last_max)
33+
ans = max(ans, last_max)
34+
35+
return ans

problems/range-sum-query-immutable.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class NumArray(object):
2+
def __init__(self, nums):
3+
#total[i] = nums[0]+nums[1]+...+nums[i]
4+
self.total = []
5+
6+
#initialize total
7+
temp = 0
8+
for num in nums:
9+
temp += num
10+
self.total.append(temp)
11+
12+
def sumRange(self, i, j):
13+
return self.total[j] - self.total[i-1] if i>0 else self.total[j]

0 commit comments

Comments
 (0)