[leetcode]股票题型123
122. 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 algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
分析:主要是寻找数组中的递减子数组,及递增子数组(或者可以认为是极值点),然后求和即可。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int size = prices.size();
if (size <= )
return ;
int index = ;
int profit = ;
while(index < size)
{
int buy,sell;
while(index+ < size && prices[index] > prices[index+]) index++;
buy = index;
while(index+ < size && prices[index]< prices[index+]) index++;
sell = index;
profit += prices[sell] - prices[buy];
index++;
}
return profit;
}
};
123. 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 algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
动态规划,preprofit保存i之前交易获得的收益。postprofit保存i之后交易获得的收益。
只能进行两次交易,在第0-i天,我进行一次交易,在第i天后我进行一次交易。
- preprofit保存,从开始那天到第i天,如果我进行交易的最大值,从max{preprofit[i-1],price[i]-curmin}获得。
从前往后扫:因为我要获得arr[0,..i](i++)包含i的最大收益:
1.要找到0-i的最小值,curmin需要更新
2.最大收益即之前的preprofit和当前price-curmin作对比。
- postprofit保存,从第i天到最后一天,如果我进行交易的最大值,从max{postprofit[i-1],curmax-postprofit[i]}获得。
从后往前扫描:因为我要获得arr[i+1……n](i++)的最大收益
1.要找到i+1~n的最大值,如果不是从后往前扫,而是从前往后扫,curmax保存的是0-i的最大值。而我们要找的是i+1~n的最大值。
2.最大收益即之后postprofit和当前的curmax-price做对比。
整个算法是这样的:是把整个数组分两段Max( MaxProfitOn[0,i] + MaxProfitOn[i+1, n-1] ),分别找最大,看看他们的和在如何分的时候才能达到全局最大。
最后遍历preprofit和postprofit数组,max{preprofit[i]+postprofit[i]}既是最大的可能收益。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int size=prices.size();
if (size<)
return ;
int curmin=prices[],curmax=prices[size-];
vector<int> preProfit(size,);
vector<int> postProfit(size,);
for(int i=;i<size;i++){
curmin=min(curmin,prices[i]);
preProfit[i]=max(prices[i]-curmin,preProfit[i-]);
}
for (int j = size-;j>=; --j){
curmax=max(curmax,prices[j]);
postProfit[j]=max(postProfit[j+],curmax-prices[j]); }
int retProfit=;
for(int m=;m<size;m++){
if(retProfit<(preProfit[m]+postProfit[m]))
retProfit=preProfit[m]+postProfit[m];
}
return retProfit;
}
};
[leetcode]股票题型123的更多相关文章
- [leetcode] 股票问题
参考文章: [1] 团灭 LeetCode 股票买卖问题 [2] Most consistent ways of dealing with the series of stock problems 其 ...
- leetcode 新题型----SQL,shell,system design
leetcode 主要是一个针对北美的coder人群找工作的代码练习网站,我在2015年初次接触这个网站的时候,总共只有200多道题目,是一个类似acm 的a题网站.这些年变化越来越大,主要是因为找工 ...
- leetcode股票问题方法收集 转载自微信公众号labuladong
一.穷举框架首先,还是一样的思路:如何穷举?这里的穷举思路和上篇文章递归的思想不太一样. 递归其实是符合我们思考的逻辑的,一步步推进,遇到无法解决的就丢给递归,一不小心就做出来了,可读性还很好.缺点就 ...
- LeetCode矩阵题型
以三角形遍历矩阵 ; i < matrix.size(); ++i) { ; j < matrix[i].size(); ++j) swap(matrix[i][j], matrix[j] ...
- leetcode 121 122 123 . Best Time to Buy and Sell Stock
121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...
- leetcode 股票系列
五道股票题总结: 121 买卖股票的最佳时机 122 买卖股票的最佳时机 124 买卖股票的最佳时机4 309 最佳股票买卖含冷冻期 714 买卖股票的最佳时机含有手续费 121 买卖股票的最佳时机 ...
- 5分钟了解二叉树之LeetCode里的二叉树
有读者反馈,现在谁不是为了找工作才学的数据结构,确实很有道理,是我肤浅了.所以为了满足大家的需求,这里总结下LeetCode里的数据结构.对于我们这种职场老人来说,刷LeetCode会遇到个很尴尬的问 ...
- java程序员学习路线阶段总结20190903
算法:锻炼写代码的逻辑 刷题位置:leetcode 书籍:小灰漫画算法 leecode使用方法: 转载自http://blog.csdn.net/tostq 又到了一年毕业就业季了,三年前的校招季我逃 ...
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
随机推荐
- sshd服务安装
SSHD服务 介绍:SSH 协议:安全外壳协议.为 Secure Shell 的缩写.SSH 为建立在应用层和传输层基础上的安全协议. 作用:sshd服务使用SSH协议可以用来进行远程控制, 或在计算 ...
- 解决ExtNET ExtJS 特定日期选择月份跳转导致无法选择月份的问题
背景 项目使用 Ext.NET 2.2.0.40838 , 对应Ext JS4.2版本. 结果 2017/3/31 号的时候偶然间点日历选择控件选择2月,10月等月份突然就跳到3月份,9月份之类. 就 ...
- webpack 配置案例for angular babel
1.dev.js: const webpack = require('webpack'); const webpackUglifyJsPlugin = require('webpack-uglify- ...
- 旷视等Oral论文提出GeoNet:基于测地距离的点云分析深度网络
基于网格曲面的几何拓扑信息可以为物体语义分析和几何建模提供较强的线索,但是,如此重要的连接性信息在点云中是缺失的.为此,旷视西雅图研究院首次提出一种全新的深度学习网络,称之为 GeoNet,可建模点云 ...
- day15(模块引用笔记)
import spam文件名是spam.py,模块名则是spam# 首次导入模块发生?件事# 1. 会产生一个模块的名称空间# 2. 执行文件spam.py,将执行过程中产生的名字都放到模块的名称空间 ...
- 免安装版本Mysql配置
免安装版本mysql配置如下:(本人使用的是5.6.42) 1. 解压后将/bin目录配置在系统变量中 2. 在mysql目录下新建my.ini文件配置如下信息: [mysqld] basedir=D ...
- list 列表 和一些操作方法
1. 什么是列表 定义: 能装对象的对象 在python中使用[]来描述列表, 内部元素用逗号隔开. 对数据类型没有要求 列表存在索引和切片. 和字符串是一样的. 2. 相关的增删改查操作(重点) 添 ...
- cdlinux写入u盘启动的制作教程
制作方法如下:(摘自于https://blog.csdn.net/suquan629/article/details/52996792) 1.所需要的工具软件: cdlinux0.9.7.isoUlt ...
- 【Linux】【Jmeter】配置Jmeter服务器和运行Jmeter
以前整理过Linux下的Jmeter知识,所以此处看可以参考以下链接: 参考链接:http://www.cnblogs.com/conquerorren/p/7880604.html [root@-- ...
- eclipse 添加svn资源库卡死。长时间等待
使用https://localhost:8443/svn/xx方式打入浏览器判断其服务器是否正常 如果正常通过,而eclipse新建库卡死时.可以等待一点时间看是否卡 问题依旧,考虑更改地址 主机名 ...