Description:

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 k transactions.

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

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

动态规划,有递推式:参考:http://blog.csdn.net/feliciafay/article/details/45128771

local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff),

global[i][j]=max(local[i][j],global[i-1][j])

public class Solution {
public int maxProfit(int k, int[] prices) { int len = prices.length; if(len < 2) {
return 0;
} if(k > len) {
return bigProfit(prices);
} int[] local = new int[k+1];
int[] global = new int[k+1]; Arrays.fill(local, 0);
Arrays.fill(global, 0); for(int i=0; i<len-1; i++) {
int diff = prices[i+1] - prices[i];
for(int j=k; j>=1; j--) {
local[j] = Math.max(global[j-1]+(diff>0?diff:0), local[j]+diff);
global[j] = Math.max(global[j], local[j]);
}
} return global[k];
} public int bigProfit(int[] prices) { int res = 0;
for(int i=0; i<prices.length-1; i++) {
if(prices[i] < prices[i+1]) {
res += prices[i+1] - prices[i];
}
} return res; } }

LeetCode——Best Time to Buy and Sell Stock IV的更多相关文章

  1. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

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

  2. LeetCode Best Time to Buy and Sell Stock IV

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...

  3. 【dp】leetcode Best Time to Buy and Sell Stock IV

    https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/ [题意] 给定n天股市的票价,最多交易k次, ...

  4. 【LeetCode】Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...

  5. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

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

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

  7. [LeetCode] 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 al ...

  8. [LeetCode] 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 ...

  9. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

随机推荐

  1. dpkg制作deb包

    deb包的文件结构: deb包里面的结构:DEBIAN目录 和 软件具体安装目录(模拟安装目录)(如etc, usr, opt, tmp等). 在DEBIAN目录中至少有control文件,还可能有p ...

  2. Struts2 的Action中取得请求参数值的几种方法

    先看GetRequestParameterAction类代码:  Java代码 public class GetRequestParameterAction extends ActionSupport ...

  3. 简单又好用的聊天室技术——WebSocket

    现在,很多网站为了实现推送技术,所用的技术都是轮询.轮询是在特定的的时间间隔(如每1秒),由浏览器对服务器发出HTTP request,然后由服务器返回最新的数据给客户端的浏览器.这种传统的模式带来很 ...

  4. 自然语言交流系统 phxnet团队 创新实训 个人博客 (二)

    因为项目用的到条件编译,遂专门记载: 众所周知在C和CPP中可以通过预处理语句来实现条件编译,但是在java中没有预处理语句,我们该如何实现条件编译呢? 这是一个简单的demo public clas ...

  5. 关于Unity中的帧动画组件的编写

    一.帧动画 1: 美术准备好一个连续动作的离散图片;2: 程序在准确的时间来切换这个图片;3: 优点: 简单,速度快; 缺点:资源占用相对过大; 二.frame_anim组件编写 1: 代码里面强制要 ...

  6. JAVA中String字符串比较equals()和equalsIgnoreCase()的区别

    标签: equals和equalsIgnoreC 2012-11-11 16:03 65644人阅读 评论(0) 收藏 举报  分类: JAVA(3)  1.使用equals( )方法比较两个字符串是 ...

  7. (转)MFC的ClistCtrl删除选中多行项目

    MFC的ClistCtrl控件添加了多行数据后,若要删除选中的多行数据,可以使用ClistCtrl的成员函数,在网上找了很多例子,发现都有问题,因为在删除ClistCtrl行的时候,删除行下面的行会上 ...

  8. FusionMap 检测融合基因

    定义:融合基因是指两个或者多个基因联合起来,一起转录形成一个转录本: 检测的意义:融合基因可以作为某些疾病的特异分子标记,比如 bcr/abl融合基因存在于95%以上的慢性粒细胞白血病患者中: AML ...

  9. c++ 的vector、array和数组的比较

    ref:  http://blog.csdn.net/haust_wang/article/details/49848169

  10. 启动vsftpd失败

    启动vsftpd失败 在使用centos时, 要用ftp上传文件, 但是一到脚本的ftp命令就会出错: rpm -Uvh http://mirror.centos.org/centos/6/os/i3 ...