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. invalid conversion from 'void* (*)()' to 'void* (*)(void*)'

    void *thread1() ], NULL, thread1, NULL)) != ) 提示:invalid conversion from 'void* (*)()' to 'void* (*) ...

  2. [转]RosBridge小结

    1.rosbridge介绍 rosbridge(rosbridge_suite)是ros官方为开发者提供的一个用于非ros系统和ros系统进行交互通信的功能包.rosbridge主要包含两个部分,Ro ...

  3. ios两视图间托付(delegate)传值

    现有两个视图(ViewController.ViewController1),从ViewController中带參数跳转到ViewController1,在ViewController1选中数据后带有 ...

  4. 查看linux硬件信息

    more /proc/cpuinfo more /proc/meminfo more /proc/*info lspci 查看主板信息等cat /proc/cpuinfo CPU信息cat /proc ...

  5. SQLServer 跨库查询实现方法

    不使用链接的服务器名,而提供特殊的连接信息,并将其作为四部分对象名的一部分 本文给出一条 SQL 语句用于展示在同一名服务器上,不同的数据库间查询,注意当前连接用户要对两个库都有权限SQL Serve ...

  6. Openfire配置过程,以及与php交互注意事项。

    Ben Werdmuller 是一位 Web 策划师和开发人员,他专注于开放源码平台.他是开源社交网络框架 Elgg 的共同创始人和技术带头人.Ben 的博客 http://benwerd.com/. ...

  7. Spring Boot 官方文档学习(二)特点

    一.SpringApplication banner,就是启动时输出的信息,可以在classpath下添加 banner.txt,或者设置 banner.location 来指向特定的文件.(默认编码 ...

  8. MVC下载远程文件流(WebClient)

    public ActionResult DownLoad_File() { return File(ScLiu(PathUrl), "application/octet-stream&quo ...

  9. PHP+tcpdf的生成

    项目中,可能用到额 查看:http://www.sucaihuo.com/php/217.html

  10. POJ 3211 Washing Clothes 背包题解

    本题是背包问题,可是须要转化成背包的. 由于是两个人洗衣服,那么就是说一个人仅仅须要洗一半就能够了,由于不能两个人同一时候洗一件衣服,所以就成了01背包问题了. 思路: 1 计算洗完同一颜色的衣服须要 ...