Leetcode Best Time to Buy and Sell Stock IV(最大子段和)
朴素的O(n^2)过不了,这里借鉴前人的思想,总结一下。
主要利用栈维护对后面产生影响的字段,然后利用贪心的思想逐步合并。
首先,找到每一对最低点(vx),最高点(px);
其次,合并当前(vx, px) 与栈内的点对(top_vx, top_px),分两种情况
1、若top_vx >= vx,考虑到vx处在更低点,(top_vx, top_px)对后面的点对不会有影响,则对(top_vx, top_px)计算价值并出栈;
2、若top_px <= px(ps:此时top_vx < vx),
a、若最优解只包含两对点的其中一对,显然可以合并两对点为(top_vx, px)最优;
b、若最优解中包含两对点,则可将两对点转化成(top_vx, px), (vx, top_ px), 其中(vx, top_ px)对后面的点对不会有影响,则对(vx, top_ px)对计算价值并出栈,同时更新(vx, px)->(top_vx, px);
最后,将栈内的点对全部计算价值(显然栈内的点对都不必合并)。
class Solution {stack< pair<int, int> > waiting;vector<int> profits;
public:int maxProfit(int k, vector<int>& pr) {int len = pr.size();if(len < 2 || k < 1) return 0;while(!waiting.empty()) waiting.pop();profits.clear();int posv, posp = -1;while(true){for(posv = posp + 1; (posv + 1) < len && pr[posv] >= pr[posv + 1]; posv++);for(posp = posv + 1; (posp + 1) < len && pr[posp] <= pr[posp + 1]; posp++);if(posp >= len) break;for(; !waiting.empty() && pr[waiting.top().first] >= pr[posv]; waiting.pop()){profits.push_back(pr[waiting.top().second] - pr[waiting.top().first]);}for(; !waiting.empty() && pr[waiting.top().second] <= pr[posp]; waiting.pop()){profits.push_back(pr[waiting.top().second] - pr[posv]);posv = waiting.top().first;}waiting.push(make_pair(posv, posp));}for(; !waiting.empty(); waiting.pop()){profits.push_back(pr[waiting.top().second] - pr[waiting.top().first]);}int ans = 0;if(k >= profits.size()) {ans = accumulate(profits.begin(), profits.end(), 0);}else{nth_element(profits.begin(), profits.begin() + k - 1, profits.end(), greater<int>());ans = accumulate(profits.begin(), profits.begin() + k, 0);}return ans;}
};
Leetcode Best Time to Buy and Sell Stock IV(最大子段和)相关推荐
- [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 ...
- 【DP + 卖股票】LeetCode 188. Best Time to Buy and Sell Stock IV
LeetCode 188. Best Time to Buy and Sell Stock IV Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyan ...
- leetcode: Best Time to Buy and Sell Stock 系列
leetcode: Best Time to Buy and Sell Stock 系列 一系列包括: - Best Time to Buy and Sell Stock Ⅰ - Best Time ...
- Leetcode Best Time to Buy and Sell Stock III
Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...
- Leetcode Best Time to Buy and Sell Stock
Leetcode Best Time to Buy and Sell Stock 相关代码,本题使用dp算法完成,本算应该算得上一个经典的dp算法题. #include <iostream> ...
- LeetCode Best Time to Buy and Sell Stock II
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目: Say you have an array ...
- [leetcode]_Best Time to Buy and Sell Stock I II
一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...
- LeetCode 188. 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 for whi ...
- LeetCode Best Time to Buy and Sell Stock(dp)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
最新文章
- java_opts gc回收器_JVM之垃圾回收机制(GC)
- Java并发7:并发工具类
- cypress测试框架与selenium_selenium自动化测试框架之PO设计模式
- 诗与远方:无题(二十八)- 曾经写给妹子的一首诗
- java的GUI之SWT框架 配置开发环境(包含但不限于WindowBuilder完整教程,解决Unknown GUI toolkit报错,解决导入SWT包错误)...
- 自带内网穿透的文件同步工具Syncthing介绍
- 怎么批量修改pdf文件名?
- 创新设计思维介绍,理解,以及学习体会
- 【Swift】图片裁剪
- wincap函数用法简述
- jQuery插件使用-瀑布流
- 怎样查看苹果服务器验证关闭,iPhone小技巧:如何查询 iOS 系统是否已经关闭验证?...
- go、JS AES(CBC模式)加密解密兼容
- Ubuntu 22.04 使用私钥登录时提示 server refused our key
- redis安装和启动
- Jetpack Compose 深入探索系列一:Composable 函数
- 集合去重,取交集并集差值
- 基于51单片机LCD1602显示
- 视通科技助力某法院审委会打造多媒体会议室
- PHP 基础知识总结
热门文章
- Output argument “yl“ (and maybe others) not assigned
- 无准考证号的四六级查询
- linux命令行的软件推荐
- Android 为应用桌面 Logo 添加数字提醒
- 立冬了,我们该怎么养生
- 写作之:syetem model和problem formulation
- html动画如何设置恢复原状态,CSS秘密花园: 动画状态
- 在一些windows平台的脚本,或者配置文件中出现的双星符号,是什么意思?
- 真阳率(true positive rate)、假阳率(false positive rate),AUC,ROC
- 3D摄影机选择指南,你知道自己需要什么样的摄影机吗?