LeetCode——Best Time to Buy and Sell Stock IV
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的更多相关文章
- [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 ...
- 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 ...
- 【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次, ...
- 【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] 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 ...
- [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 ...
- [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 ...
- [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 ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
随机推荐
- HTML——如何在html中插入视频
1,插入优酷视频: 在优酷分享界面有个html代码,直接复制放入body中,定义div的align居中即可 2.插入本地视频:用video属性 用mp4格式 <video>标签的属性 s ...
- Phalcon Framework的Mvc结构及启动流程(部分源码分析)
创建项目 Phalcon环境配置安装后,可以通过命令行生成一个标准的Phalcon多模块应用 phalcon project eva --type modules入口文件为public/index.p ...
- MongoDB 集群搭建(主从复制、副本及)(五)
六:架构管理 mongodb的主从集群分为两种: 1:master-Slave 复制(主从) --从server不会主动变成主server,须要设置才行 2:replica Sets 复制(副本 ...
- [从jQuery看JavaScript]-匿名函数与闭包
jQuery片段: (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的第一眼,我 ...
- Qt/C++ 构造函数与explicit
1.默认构造函数 默认构造函数是指所有参数都提供了默认值的构造函数,通常指无参的构造函数或提供默认值的构造函数.如类Test1和Test2的构造函数 class Test1 { public: Tes ...
- 【转】C# 二维码生成
/// <summary> /// 含有QR码的描述类和包装编码和渲染 /// </summary> public class QRCodeHelper { /// <s ...
- Android startActivityForResult 回传数据
一个activity打开新的activity,新的activity关闭之后,返回数据.原来的activity要接收返回的数据,在开启新的activity时,就需要调用startActivityForR ...
- javax.naming.NoInitialContextException错误的解决方案
今天,学习用了一下nutz框架,写了一个HelloWorld的小程序,在用jndi配置数据源时,写了一个测试类,并在main方法中调用了jndi获得数据库连接,但是报错了,错误信息如下: javax. ...
- ASP.NET自带对象JSON字符串与实体类的转换
关于JSON的更多介绍,请各位自行google了解!如果要我写的话,我也是去Google后copy!嘿嘿,一直以来很想学习json,大量的找资料和写demo,总算有点了解! 切入正题! 还是先封装一个 ...
- module、applet
Each Module is developed as a standalone Windows DLL.Each module can contain one or more applets, an ...