这是这个系列题目的第四个,题目大意和之前的差不多,但是这次提供最多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. Python——使用高德API获取POI(以深圳南山医疗保健服务POI为例)

    以下内容为原创,转载请注明出处. import xlwt #创建Excel,见代码行8,9,11,25,28:CMD下:运行pip install xlwt进行安装 import urllib.req ...

  2. navicat连接IEE数据库查询话单

    select * from cdl_raw_1x_12501_ztev8_sht_201811 t1 WHERE ( t1.call_start_time >= STR_TO_DATE( '20 ...

  3. 好的java资源地址

    前人栽树,后人乘凉.想当初自己初学Java时为了解决一个很基础的问题,好多的朋友热心的回复我,帮我分析错误.现在为了方便那些Java新手,特给出自己感觉比较好的学习网站和论坛,希望对朋友们能有点帮助. ...

  4. [经验交流] 影响 kubernetes 稳定性的因素

    使用k8s已有近一年的时间,版本从1.2到1.5.1.6.1.7,期间出现并解决了不少问题,下面是我总结的影响k8s集群稳定性的因素: 1. 安装环境 *kubelet版本最好与kube-apiser ...

  5. 「JavaScript面向对象编程指南」闭包

    闭包 JS只有函数作用域,函数外为全局变量,函数内为局部变量 绿圆是函数fn的作用域,在这范围内可访问局部变量b和全局变量a,橙圆是fn内部函数inner的作用域,此范围内可访问自身作用域内的变量c, ...

  6. 帆软报表(finereport)间格运算常用公式

    1.1在C3(占比)单元格中直接使用占比公式:=PROPORTION(B3):占比:当前值占总值的比例 1.2 计组内占比注:C2[!0]{A2=$A2},表示C2扩展出来地区相同的单元格.sum(C ...

  7. HTML基本语法

    一.什么是HTML? HTML不是编程语言,是用来描述网页文档(页面结构)的一种标记语言: HTML指超文本标记语言(Hyper Text Markup Language),之所以称为超文本标记语言, ...

  8. C# EntityFramework Code First 迁移 降级 回退到空数据库

    C# EntityFramework  Code First 迁移 降级 回退到空数据库 1.包管理器控制台-迁移 在包管理器控制台中运行 Enable-Migrations Add-Migratio ...

  9. hadoop mkdir: Cannot create directory /usr. Name node is in safe mode.

    今天在hdfs上面创建文件夹的时候报了:org.apache.hadoop.dfs.SafeModeException: Cannot delete /user/hadoop/input. Name ...

  10. 【微信小程序canvas】实现小程序手写板用户签名(附代码)

    工作中公司业务需要的微信小程序用户签字功能 先看效果图: wxml: <view class="wrapper"> <view class="handB ...