动态规划——Best Time to Buy and Sell Stock IV
状态转移方程: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的更多相关文章
- 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. ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- 【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 ...
- 【刷题-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 ...
- 动态规划——Best Time to Buy and Sell Stock III
题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格. 如果最多进行两次交易,但必须在买进一只股票前清空手中的股票,求最大的收益. 示例 1:Input: [3,3,5,0,0,3 ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- GCC 警告
-w -W禁止/开启 编译警告的打印.这个警告不建议使用.大约2012年底,公司代码进行一次大重构,另外从Codeblock集成开发环境转向Makefile管理,Makefile里面默认使用了-w,因 ...
- 洛谷红名+AC150祭
emmmm没什么想说的,随便放个图吧23333(逃~
- iMX6Q开发板的EIM接口的配置可以与FPGA通讯-交换数据-最常用的接口配置
最近基于迅为的i.mx6Q开发板进行了一个FPGA项目的开发,下面给大家介绍一下EIM接口的配置,包括引脚的的选择和寄存器的配置 For the usage of WEIM on i.MX6, you ...
- 「IOI2018」狼人
快咕一个月了 咕咕咕 咕咕咕咕 LOJ #2865 Luogu P4899(离线) UOJ #407(强制在线) 题意 给定一棵树和若干组询问$(S,E,L,R)$ 表示你初始在$S$,想到达$E$, ...
- 「JavaScript面向对象编程指南」闭包
闭包 JS只有函数作用域,函数外为全局变量,函数内为局部变量 绿圆是函数fn的作用域,在这范围内可访问局部变量b和全局变量a,橙圆是fn内部函数inner的作用域,此范围内可访问自身作用域内的变量c, ...
- Git(1):版本库+工作区+暂存区
参考博客:https://blog.csdn.net/qq_27825451/article/details/69396866
- sed 修改文本
修改文本是指将所匹配的文本行利用新文本替代,sed编辑命令的修改文本符号为 c\, [ sed]$ more input [ sed]$ more aa.sed #!/bin/sed -f //c\ ...
- Gitlab_ansible_jenkins三剑客④jenkins安装图解及freestyle的简单使用
java环境准备 # 安装jdk1.8 [root@node02 ~]# rpm -ivh jdk-8u181-linux-x64.rpm vim /etc/profile export JAVA_H ...
- goroute应用-模拟远程调用RPC
go语言简单模拟RPC,详见个人新博客:blog.dlgde.cn 代码如下: package main import ( "errors" "fmt" &qu ...
- nginx+uwsgi+django开发环境搭建
Nginx+uWSGI+Djangoi开发环境搭建 Django简介,环境搭建 uWSGI简介,安装与配置 Nginx安装与配置 Nginx+uWSGI+Django原理解析 1.django简介,环 ...