这是这个系列题目的第四个,题目大意和之前的差不多,但是这次提供最多k次的操作,操作还是不能同时操作即必须结束前一个操作才能进行后一个操作。
状态比较好理解,就是题目要求的缩小版,dp[k][i]表示进行到第i天时最多k次操作能得到的最大利益,但是要注意最后一次操作不一定时第i天完成的。
状态转移方程:dp[k][i] = max(dp[k][i-1],dp[k-1][j]+prices[i]-prices[j]) (0<=j<=i)
比如现在正在考虑dp[k][i],选择有两种,一是第i天不操作,二是在第k-1次第j天后进行第k次操作。这个题的状态转移方程相对来说还是比较容易理解的。
当然如果给的操作数比总天数的一半还要多的话,相邻两天一次交易即可,当然要保证后一天的价格比前一天的价格要高。这种情况就不用再动态的递推了,直接累加即可。
 class Solution{
public static int maxProfit(int k,int[] prices) {
int nlen = prices.length;
if(nlen<=1)return 0;
else if(k>nlen/2) {
int res = 0;
for(int i = 1;i<nlen;i++)
if(prices[i]>prices[i-1])res+=(prices[i]-prices[i-1]);
return res;
}else {
int temp = 0;
int[][]dp = new int[k+1][nlen];
Arrays.fill(dp[0],0);
for(int i = 0;i<=k;i++)
dp[i][0] = 0;
for(int kt = 1;kt<=k;kt++) {
for(int i = 1;i<nlen;i++) {
temp = 0;
for(int j = 0;j<=i;j++)
temp = temp>(dp[kt-1][j]+prices[i]-prices[j])?temp:(dp[kt-1][j]+prices[i]-prices[j]);
dp[kt][i] = dp[kt][i-1]>temp?dp[kt][i-1]:temp;
}
}
return dp[k][nlen-1];
}
}
}

不过在最后说一句,由于我编写的代码使用了三层for循环,时间复杂度为O(n^3),在LeetCode上仅仅击败了不到9%的人,额,又是一个比较惨淡的数据了。。。。

动态规划——Best Time to Buy and Sell Stock IV的更多相关文章

  1. Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)

    Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...

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

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

  3. 【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 ...

  4. 【刷题-LeetCode】188 Best Time to Buy and Sell Stock IV

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

  5. 动态规划——Best Time to Buy and Sell Stock III

    题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格. 如果最多进行两次交易,但必须在买进一只股票前清空手中的股票,求最大的收益. 示例 1:Input: [3,3,5,0,0,3 ...

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

  7. Lintcode393 Best Time to Buy and Sell Stock IV solution 题解

    [题目描述] Say you have an array for which the i th element is the price of a given stock on day i. Desi ...

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

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

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

随机推荐

  1. 基于jeesite的cms系统(七):GlobalException全局异常和部署

    关于全局异常: 在业务代码中专注处理业务,而不是返回各种CodeMsg(比如这里只需要知道登录时成功还是失败,其余情况直接抛出异常),可以直接抛出异常,添加一个全局异常类,根据CodeMsg来生成异常 ...

  2. DOS:第一天

    cd,有时也写作chdir(change directory,改变目录),是在Unix.Windows和DOS操作系统下用于改变工作目录的命令行命令.在Unix的外壳脚本与Windows或DOS的批处 ...

  3. IIS中报错弹出调试,系统日志-错误应用程序名称: w3wp.exe,版本: 8.5.9600.16384,时间戳: 0x5215df96(360主机卫士)

    偶遇一次特殊情况,在使用Web系统导入数据模版(excel)时,服务端IIS会报错并弹出调试框,然后整个网站都处于卡死的debug状态,如果点否不进行调试,则IIS会中断调试,Web系统继续执行,运行 ...

  4. ztree树应用

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ZtreeListVi ...

  5. Bootstrap使用-1

    目录 结构: 1. 视图函数 2. 模板 3. 登陆 4. 怎样发生的 添加模板 基础模板提供的block 定制基础模板 结构: $ tree -I "__pycache*|*.pyc|*. ...

  6. P5304 [GXOI/GZOI2019]旅行者

    题目地址:P5304 [GXOI/GZOI2019]旅行者 这里是官方题解 一个图 \(n\) 点 \(m\) 条边,里面有 \(k\) 个特殊点,问这 \(k\) 个点之间两两最短路的最小值是多少? ...

  7. 区别 chown和chmod的用法

    本人总是习惯使用chmod,而把chown混淆. chown就是修改 第一列内容的 ,chmod是修改 第3,4列内容的. chown用法用来更改某个目录或文件的用户名和用户组的chown 用户名:组 ...

  8. Asp.Net Core配置Swagger

    本文主要参考:Using Swagger with ASP.net Core 1.创建WebApi项目 本文使用ASP.Net Core Web API项目模板演示Swagger到使用,首先创建Web ...

  9. python2 使用pip安装psycopg2出现错误:Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-mvzdNj/psycopg2/

    公司业务需求,开发语言python2,需要使用数据库:postgresql,需要安装模块psycopg2这个模块, 使用pip install psycopg2 报错: Command "p ...

  10. Nginx负载-nginx转发到Swoole服务器(nginx配置文件变更)