code121:Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Example 1:
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
Example 2:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
解答:
通俗理解情况:
1 连续减小。拐点是存在增大的点。即寻找初始点为增大的点,前者为low,后者为high
2 对于后续新增点情况的考虑:
1)比high大,则更新max_pro
2)比low小,存储进temp,新点需要和temp取差值,然后决定max_pro。大的时候更新low为temp以及max_pro
3)介于之间则continue
代码:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices)<=1:
return 0
i=1
init = prices[0]
while i<=len(prices):
if i == len(prices):
return 0
if prices[i]<init:
init = prices[i]
i+=1
else:
break
cur_low = prices[i-1]
temp = prices[i-1]
cur_high = prices[i]
max_pro = cur_high - cur_low
for j in range(i+1,len(prices)):
cur_max = prices[j]-cur_low
max_pro = max(max_pro,cur_max)
#print(max_pro)
if prices[j]-temp> max_pro:
max_pro = prices[j]-temp
#print(max_pro,temp)
cur_low = temp
else:
if prices[j]<temp:
temp = prices[j]
else:
continue
return max_pro
代码过于复杂。观察后发现,temp比low小或者等于,即新进点如果大于high,low都要变更为temp。(也就是说,low应该为新进点前数组的min)
但是这个差值可能不一定比max_pro大。
这里就可以使用KA的想法解题。
存储low为当前的最小值。存储当前利润为cur-low。存储最大值为max(max_pro,cur_pro)。遍历即可解决问题。
代码:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_pri,max_pro = float('inf'),0
for price in prices:
min_pri = min(min_pri,price)
cur_pro = price - min_pri
max_pro = max(max_pro,cur_pro)
return max_pro
code122: Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
解答:
观察情况,出现低谷再出现峰值时,我们会卖出袜子。
1 def是否低谷
2 def是否峰值
3 遍历 先低谷再峰值,累加其差(主要细节)
代码:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
i = 0
is_min = False
is_max = False
profit = 0
while i <= len(prices) - 1:
while i <= len(prices) - 1 and not is_min:
is_min = self.is_min(prices, i)
i += 1
cur_low = prices[i - 1]
if i == len(prices):
break
is_min = False
while i <= len(prices) - 1 and not is_max:
is_max = self.is_max(prices, i)
i += 1
cur_high = prices[i - 1]
is_max = False
profit += (cur_high - cur_low)
# print(profit)
return profit
def is_max(self, arr, i):
if i == len(arr) - 1:
return True
if arr[i - 1] <= arr[i] and arr[i + 1] <= arr[i]:
return True
else:
return False
def is_min(self, arr, i):
# print(i,i+1,i-1)
if i == len(arr) - 1:
return True
if i == 0 and arr[i] <= arr[i + 1]:
return True
if arr[i + 1] >= arr[i] and arr[i - 1] >= arr[i]:
return True
else:
return False
比较繁琐(虽然时间复杂度差不多)
简化:
峰值减去低谷:代表的意思是 只有数列出现增长行为 我们就要把增值加上。
这样就简单了:一次遍历,寻找i+1大于i的情况,差值累加。
代码:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
for i in range(len(prices)-1):
if prices[i+1]>prices[i]:
profit+=(prices[i+1]-prices[i])
return profit