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 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).
分析
依然是stock problem模型,但这次必须压缩空间,否则超限,并且在 k>prices.size() 时,转化为k=+无穷的情况,否则超限。
const int inf=-999999;
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if(prices.size()<=1) return 0;
if (k >= prices.size()) {
int T_ik0 = 0, T_ik1 =inf;
for (auto price : prices) {
T_ik1 = max(T_ik1, T_ik0 - price);
T_ik0 = max(T_ik0, T_ik1 + price);
}
return T_ik0;
}
int sell[k+1]={0},buy[k+1];
fill(buy,buy+k+1,inf);
for(int i=0;i<prices.size();i++)
for(int j=1;j<=k;j++){
sell[j]=max(sell[j],buy[j]+prices[i]);
buy[j]=max(buy[j],sell[j-1]-prices[i]);
}
return sell[k];
}
};
LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)的更多相关文章
- 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] 121. 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] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 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] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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] 309. 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】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【刷题-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 3 (hard) 自己做出来了 但别人的更好
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- ios手机Safari本地服务连不上
问题: 今天在本地起服务准备测下ios手机端页面,结果发现:页面可以打开,但是登录不上. 用alert定位了下,await fn() 报错被try()catch(){}捕获了... 原因: 该机子不支 ...
- 190 Reverse Bits 颠倒二进制位
颠倒给定的32位无符号整数的二进制位.例如,给定输入 43261596(二进制表示为 00000010100101000001111010011100 ),返回 964176192(二进制表示为 00 ...
- [转]如何在 TFS 中使用 Git
本文转自 http://www.cnblogs.com/stg609/p/3651688.html 对 Charley Blog 的代码进行版本控制的想法由来已久,在代码建立之初其实已经使用过 TFS ...
- net start iisadmin报错:系统找不到指定的文件
IIS Admin Service不能启动 ,直接启动或命令(net start iisadmin)都不成功.导致IIS站点访问异常. 最终参考网上解决方案: 这是大多是由于windows\syste ...
- Java实现三角形计数
题: 解: 这道题考的是穷举的算法. 一开始看到这道题的时候,本能的想到用递归实现.但使用递归的话数据少没问题,数据多了之后会抛栈溢出的异常.我查了一下,原因是使用递归创建了太多的变量, 每个变量创建 ...
- dubbo系列--集群容错
作为一个程序员,咱们在开发的时候不仅仅是完成某个功能,更要考虑其异常情况程序如何设计,比如说:dubbo的消费端调用服务方异常的情况,要不要处理?如何处理? dubbo提供了多种集群容错机制,默认是f ...
- vs2015 qt5.8新添加文件时出现“无法找到源文件ui.xxx.h”
转载请注明出处:http://www.cnblogs.com/dachen408/p/7147135.html vs2015 qt5.8新添加文件时出现“无法找到源文件ui.xxx.h” 暂时解决版本 ...
- 迅为7寸工业触摸屏嵌入式平台可用于ARM嵌入式一体机
7寸触摸屏介绍产品名称:7寸IPS高清屏幕分辨率:1280*800触摸屏类型:电容屏(五点触摸)接线方式: FPC 可应用于嵌入式一体机.自动售货机.广告机.智能自动终端.零售终端等 ARM平台处理器 ...
- jQuery 返回顶部效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C#在Excel的簡單操作--適用:與DB數據的簡單交互
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...