LeetCode-188.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 algorithm to find the maximum profit. You may complete at most ktransactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Example 1:
- Input: [2,4,1], k = 2
- Output: 2
- Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
Example 2:
- Input: [3,2,6,5,0,3], k = 2
- Output: 7
- Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.
- Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
- 使用动态规划
- public int maxProfit(int k, int[] prices) {//dp mytip
- if(null==prices||0==prices.length||0>=k){
- return 0;
- }
- int max =0;
- if(k>prices.length){//若k过大 优化
- for (int i = 0; i < prices.length-1; i++) {
- if(prices[i]<prices[i+1]){
- max+= prices[i+1]-prices[i];
- }
- }
- }
- else{
- int[][][] states = new int[prices.length][k+1][2];//状态表示第i个数在第j此交易中,有无股票时(0为无,1为有)的利益;//因为只保存上一个数时的利益,所以states可优化为[k+1][2]
- for(int i=0;i<=k;i++){//初始化第1个数的状态
- states[0][i][1]=-prices[0];
- }
- for(int i=1;i<prices.length;i++){
- for(int j=0;j<=k;j++){
- if(j==0){
- states[i][j][0] = states[i-1][j][0];//防止j-1溢出
- }
- else{
- states[i][j][0] = Math.max(states[i-1][j][0],states[i-1][j-1][1]+prices[i]);
- }
- states[i][j][1] = Math.max(states[i-1][j][1],states[i-1][j][0]-prices[i]);
- }
- }
- for(int i=0;i<=k;i++){
- max = max>states[prices.length-1][i][0]?max:states[prices.length-1][i][0];
- }
- }
- return max;
- }
优化空间
- public int maxProfit(int k, int[] prices) {//dp my
- if(null==prices||0==prices.length||0>=k){
- return 0;
- }
- int max =0;
- if(k>prices.length){
- for (int i = 0; i < prices.length-1; i++) {
- if(prices[i]<prices[i+1]){
- max+= prices[i+1]-prices[i];
- }
- }
- }
- else{
- int[][] states = new int[k+1][2];
- states[0][1] = -prices[0];
- for(int i=0;i<=k;i++){
- states[i][1]=-prices[0];
- }
- for(int i=1;i<prices.length;i++){
- for(int j=0;j<=k;j++){
- states[j][1] = Math.max(states[j][1],states[j][0]-prices[i]);
- if(j==0){
- states[j][0] = states[j][0];
- }
- else{
- states[j][0] = Math.max(states[j][0],states[j-1][1]+prices[i]);
- }
- }
- }
- for(int i=0;i<=k;i++){
- max = max>states[i][0]?max:states[i][0];
- }
- }
- return max;
- }
相关题
买卖股票的最佳时间1 LeetCode121 https://www.cnblogs.com/zhacai/p/10429264.html
买卖股票的最佳时间2 LeetCode122 https://www.cnblogs.com/zhacai/p/10596627.html
买卖股票的最佳时间3 LeetCode123 https://www.cnblogs.com/zhacai/p/10645571.html
买卖股票的最佳时间冷冻期 LeetCode309 https://www.cnblogs.com/zhacai/p/10655970.html
买卖股票的最佳时间交易费 LeetCode714 https://www.cnblogs.com/zhacai/p/10659288.html
LeetCode-188.Best Time to Buy and Sell Stock IV的更多相关文章
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [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 ...
- LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【刷题-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 ...
- 【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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- 188. Best Time to Buy and Sell Stock IV (Array; DP)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 188. Best Time to Buy and Sell Stock IV leetcode解题笔记
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 188. Best Time to Buy and Sell Stock IV——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- Java如何清除空格?
在Java编程中,如何清除/删除空格? 以下示例演示如何使用Util.regex.Pattern类的matcher.replaceAll(stringname)方法来删除空格. package com ...
- Linux系统排查4——网络篇
用于排查Linux系统的网络故障. 网络排查一般是有一定的思路和顺序的,其实排查的思路就是根据具体的问题逐段排除故障可能发生的地方,最终确定问题. 所以首先要问一问,网络问题是什么,是不通,还是慢? ...
- Memcached 总结 启动多个Memcached服务 配置文件详解
一. 1.解压下载的安装包到指定目录. 2.服务安装,使用管理员权限运行以下命令: c:\memcached\memcached.exe -d install 二.同一台Windows机器中启动多个M ...
- mysql数据库建立的数据库在哪个文件夹?
一般在安装目录下的data文件夹下,或者在C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.1\dat ...
- UML中类结构图示例
- C#实现如何判断一个数组中是否有重复的元素
如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hashtable的Contains方法进行查找 /// ...
- nginx 自启动脚本
nginx 自启动脚本 创建脚本 cd /etc/init.d vi nginx 脚本如下: #! /bin/bash # chkconfig: 35 85 15 # description: Ngi ...
- day_4.23 简易计算器
''' 简易加减乘除计算器demo 2018-4-23 19:32:49 ''' #1.界面 print("="*50) print(" 欢迎使用计算器v0.1" ...
- E - TOYS
来源 poj 2318 Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad ...
- shell参数扩展
http://zuyunfei.com/2016/03/23/Shell-Truncate-File-Extension/