leetcode 【 Best Time to Buy and Sell Stock III 】python 实现

题目

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 at most two transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

代码:Runtime: 175 ms

 1 class Solution:
 2     # @param prices, a list of integer
 3     # @return an integer
 4     def maxProfit_with_k_transactions(self, prices, k):
 5         days = len(prices)
 6         local_max = [[0 for i in range(k+1)] for i in range(days)]
 7         global_max = [[0 for i in range(k+1)] for i in range(days)]
 8         for i in range(1,days):
 9             diff = prices[i] - prices[i-1]
10             for j in range(1,k+1):
11                 local_max[i][j] = max(local_max[i-1][j]+diff, global_max[i-1][j-1]+max(diff,0))
12                 global_max[i][j] = max(local_max[i][j], global_max[i-1][j])
13         return global_max[days-1][k]
14
15     def maxProfit(self, prices):
16         if prices is None or len(prices)<2:
17             return 0
18         return self.maxProfit_with_k_transactions(prices, 2)

思路

不是自己想的,参考这篇博客http://blog.csdn.net/fightforyourdream/article/details/14503469

跟上面博客一样的思路就不重复了,下面是自己的心得体会:

1. 这类题目,终极思路一定是往动态规划上靠,我自己概括为“全局最优 = 当前元素之前的所有元素里面的最优 or 包含当前元素的最优”

2. 这道题的动归的难点在于,只靠一个迭代公式无法完成寻优。

思路如下:

global_max[i][j] = max( global_max[i-1][j], local_max[i][j])

上述的迭代公式思路很清楚:“到第i个元素的全局最优 = 不包含第i个元素的全局最优 or 包含当前元素的局部最优”

但问题来了,local_max[i][j]是啥?没法算啊~

那么,为什么不可以对local_max[i][j]再来一个动态规划求解呢?

于是,有了如下的迭代公式:

local_max[i][j] = max(local_max[i-1][j]+diff, global_max[i-1][j-1]+max(diff,0))

上面的递推公式 把local_max当成寻优目标了,思路还是fellow经典动态规划思路。

但是,有一部分我一开始一直没想通(蓝字部分),按照经典动态规划思路直观来分析,就应该是local_max[i-1][j]啊,怎么还多出来一个diff呢?

时间: 2024-10-10 15:21:48

leetcode 【 Best Time to Buy and Sell Stock III 】python 实现的相关文章

[leetcode]Best Time to Buy and Sell Stock III @ Python

原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: 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 at most two transa

leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)

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 at most two transactions. Note: You may not engage in multiple transactions at the same time (ie,

LeetCode: Best Time to Buy and Sell Stock III [123]

[题目] 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 at most two transactions. Note: You may not engage in multiple transactions at the same time

LeetCode——Best Time to Buy and Sell Stock III

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 at most two transactions. Note: You may not engage in multiple transactions at the same time (ie,

LeetCode: Best Time to Buy and Sell Stock III 解题报告

Best Time to Buy and Sell Stock IIIQuestion SolutionSay 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 at most two transactions. Note:You may not en

[LeetCode]Best Time to Buy and Sell Stock III 动态规划

本题是Best Time to Buy and Sell Stock/的改进版. 本题中,可以买最多买进卖出两次股票. 买两次股票可以看成是第0~i天买进卖出以及第i+1~n-1天买进卖出两部分.这要枚举i并求出0th~ith的最大利益与(i+1)th~(n-1)th的最大利益之和的最大值就是买进卖出两次可以得到的最大利益.即状态转移方程: dp[0,n-1]=max{dp[0,k]+dp[k+1,n-1]},k=1,...,n-2 而只买进卖出一次的最大利润通过0th~ith可以求得. 这里求

[Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机

Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (ie

[LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

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 at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,

[leetcode]Best Time to Buy and Sell Stock II @ Python

原题地址:https://oj.leetcode.com/problems/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 transaction

LeetCode&mdash;&mdash;Best Time to Buy and Sell Stock III (股票买卖时机问题3)

问题: 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 at most two transactions. 依旧还是那个熟悉的味道,如果II是I的另一个版本,那么III就是他们的一个升级版. 在这个版本中只能交易两次,也就是买卖各两次,其它条