题目

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

 class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit_with_k_transactions(self, prices, k):
days = len(prices)
local_max = [[0 for i in range(k+1)] for i in range(days)]
global_max = [[0 for i in range(k+1)] for i in range(days)]
for i in range(1,days):
diff = prices[i] - prices[i-1]
for j in range(1,k+1):
local_max[i][j] = max(local_max[i-1][j]+diff, global_max[i-1][j-1]+max(diff,0))
global_max[i][j] = max(local_max[i][j], global_max[i-1][j])
return global_max[days-1][k] def maxProfit(self, prices):
if prices is None or len(prices)<2:
return 0
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呢?

===========================================================================================================

时隔几天再想想,求解local_max[i][j]的过程其实并不能算传统动态规划的思路,之前的思路有些偏差。原因是local_max本身就不是一个“全局”最优,因为计算local[i][j]的时候就已经把最近的一个元素算进去了。local_max[i][j] = max(local_max[i-1][j]+diff, global_max[i-1][j-1]+max(diff,0))这个公式的得来,也真心是原作者巧妙分析的结果,一下就解决了求解N次交易最优的问题。只能膜拜并记住这部分代码。

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

  1. [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 ...

  2. 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 ...

  3. [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 al ...

  4. [LeetCode] Best Time to Buy and Sell Stock III

    将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...

  5. 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 ...

  6. [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 a ...

  7. [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 ...

  8. leetcode -- Best Time to Buy and Sell Stock III TODO

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. LeetCode——Best Time to Buy and Sell Stock III

    Description: Say you have an array for which the ith element is the price of a given stock on day i. ...

  10. LeetCode——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 a ...

随机推荐

  1. 构建第一个Spring Boot2.0应用之项目创建(一)

     1.开发环境 IDE: JAVA环境: Tomcat: 2.使用Idea生成spring boot项目 以下是使用Idea生成基本的spring boot的步骤. (1)创建工程第一步 (2)创建工 ...

  2. Android自定义控件练手——简单的时钟

    首先这应该是一个老生常谈的设计了,但是毕竟身为小白的自己都没动手做过,不动手怎么提高自己呢,所以在这梅林沉船闲暇之际,我就把我的设计流程与思路记录下来.首先来看看效果图吧: 如上图就是一个简单并没有美 ...

  3. iOS开发 - 在状态栏显示FPS,CPU和内存信息

    原理 FPS的计算 CoreAnimation有一个很好用的类CADisplayLink,这个类会在每一帧绘制之前调用,并且可以获取时间戳.于是,我们只要统计出,在1s内的帧数即可. - (void) ...

  4. 【MFC】可以换行的编辑框

    在mfc中编辑框允许输入多行时,换行符被表示为<归位><换行>即“\r\n”,用ascii码表示为13 10 如果为编辑框中想要输入换行,就请将编辑框的属性: Auto HSc ...

  5. ajax请求成功后js刷新当前页,当前页是post查询结果(用post请求进行搜索筛选)的问题

    下面的一个ajax操作,原先操作成功会刷新当前页,保证用户看到的数据是最新的,一般情况不会出现问题.$.ajax({ url: url + "/addTeacherAuth", / ...

  6. Euerka环境搭建

    机器环境 windows10,IntelliJ IDEA 配置host 单节点Eureka 一.pom文件 <?xml version="1.0" encoding=&quo ...

  7. select into outfile

    语法格式如下: SELECT [列名] FROM table [WHERE 语句]         INTO OUTFILE '目标文件' [OPTION];   FIELDS TERMINATED ...

  8. 编译防火墙——C++的Pimpl惯用法解析

    http://blog.csdn.net/lihao21/article/details/47610309 Pimpl(pointer to implementation, 指向实现的指针)是一种常用 ...

  9. iOS 常用正则表达式

    今天看到一个正则表达式的文章,总结的挺好的,就自己转载一下,我还会陆续加入一些我自己看到常用的正则表达式 (原地址:http://www.code4app.com/blog-721976-112.ht ...

  10. 2017.12.10 Java写一个杨辉三角(二维数组的应用)

    杨辉三角的定律 第n行m列元素通项公式为: C(n-1,m-1)=(n-1)!/[(m-1)!(n-m)!] 需要用到创建二维数组 package com.glut.demo; /** * 杨辉三角 ...