[LeetCode 题解]:Best Time to Buy and Sell Stock
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
2. 题意
给定一个整数数组Array用以表示某股票在每一天的价格,即该数组中第i个元素表示第i天的价格。
假如用户在给定的天数内只能完成一次交易(即在某一天购买该股票,然后在另一天将其抛售)。请设计一个算法,使得用户在此交易过程中获得最大利润。
3. 思路
规则:一只股票必须先买入,然后再售出。
采用动态规划的思路:
使用两个变量记录当前最小值minPrice,以及当前最大值maxPrice。
Profit的递归过程,可以按照如下状态变换方程表示:

4: 解法
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.size()<=1) return 0;
int min=prices.front(); // 当前最小值
int max=prices.front(); //当前最大值
int maxP=0; //最大profit
for(int i=1;i<prices.size();i++){
if(prices[i]>max){ //当前值大于max
maxP=prices[i]-min;
max=prices[i];
}else if(prices[i]<min){ // 当前值比min小,那么更新min
min=prices[i];
}else{
if(prices[i]-min>maxP){ // 当前值介于min~max之间
maxP=prices[i]-min;
max=prices[i];
}
}
}
return maxP;
}
};
[LeetCode 题解]:Best Time to Buy and Sell Stock的更多相关文章
- 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] 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] 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] 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 Week6]Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...
- 【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][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
随机推荐
- 11_java之接口和多态
01接口的概念 * A:接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来 ...
- nginx root与alias区别
引用于文章https://blog.csdn.net/line_aijava/article/details/71473793 root: Sets the root directory for re ...
- 解读linux中用户密码规则及忘记root口令的破解(思路)
linux当中,用户名和密码表对应关系放在/etc/passwd中,如: root:x:0:0:root:/root:/bin/bash 格式代表意义分别为 用户名:密码:用户id:组id:用户描述 ...
- fatal error C1010: 在查找预编译头时遇到意外的文件结尾 (转)
错误描述:fatal error C1010: 在查找预编译头时遇到意外的文件结尾.是否忘记了向源中添加“#include "stdafx.h"”? 错误分析: 此错误发生 ...
- IDEA2018.2版本注册
IntelliJ IDEA 2018.2版本注册 1.到官网下载IDEA安装文件,windows版本ideaIU-2018.2.2.exe,然后安装: 2.下载补丁包JetbrainsCrack-3. ...
- iOS学习之UIDatePicker控件使用
iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. , 您可以选择自己需要的模式,Time, Date,Date and Time , Count Down Ti ...
- Lambert模型
[Lambert模型] 漫反射光的强度近似地服从于Lambert定律,即漫反射光的光强仅与入射光的方向和反射点处表面法向夹角的余弦成正比. 由此可以构造出Lambert漫反射模型:Idiffuse = ...
- go_变量定义
package main import "fmt" var( aa =3 bb ="kkk" cc =true )//go语言中,变量可以定义在函数外面,并不是 ...
- 解剖Nginx·自动脚本篇(4)工具型脚本系列
目录 auto/have 向自动配置头文件追加可用宏定义(objs/ngx_auto_config.h) auto/nohave 向自动配置头文件追加不可用宏定义(objs/ngx_auto_conf ...
- SQL 数据库 学习 007 通过一个示例简单介绍什么是字段、属性、列、元组、记录、表、主键、外键 (上)
SQL 数据库 学习 007 通过一个示例简单介绍什么是字段.属性.列.元组.记录.表.主键.外键 (上) 我们来介绍一下:数据库是如何存储数据的. 数据库是如何存储数据的 来看一个小例子 scott ...