[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 ...
随机推荐
- Linux下编译、安装并启动memcached
首先使用yum安装gcc make.autoconf.libtool系列工具,这几个工具是编译所需要的,命令如下: yum install gcc make autoconf libtool 然后到l ...
- Delegate(代理)异常:该委托必须有一个目标
转自 Delegate(代理)异常:该委托必须有一个目标 在代理调用BeginInvoke(new AsyncCallback(callBack), null);时,会抛这个异常的原因是该代理变量代理 ...
- nginx的location和rewrite
1 Nginx rewrite基本语法 Nginx的rewrite语法其实很简单.用到的指令无非是这几个 set if return break rewrite 麻雀虽小,可御可萝五脏俱全.只是简单的 ...
- ElasticSearch的java api
pom <dependencies> <dependency> <groupId>org.elasticsearch.client</groupId> ...
- MySQL 基础常用命令
一.启动与关闭 1.1 Linux下启动mysql 的命令: a. rpm包安装:service mysqld start b. 源码包安装:/usr/local/mysql/bin/mysqld_s ...
- H5/
1.value: 2.selected="selected": 设置selected="selected"属性,则该选项就被默认选中. 下拉列表也可以进行多选操 ...
- Cannot subclass final class class com.sun.proxy.$Proxy16
Cannot subclass final class class com.sun.proxy.$Proxy16 2016年05月04日 19:10:58 阅读数:15028 背景 这个错误是我在使用 ...
- AspectJ AOP介绍
idea下aspectj程序运行示例 有些同学可能想自己编写aspect程序进行测试练习,博主在这简单介绍运行环境的搭建,首先博主使用的idea的IDE,因此只对idea进行介绍.首先通过maven仓 ...
- 关于super关键字
1.在Java中,有时会遇到子类中的成员变量或方法与父类中的成员变量或方法同名.此时父类的成员变量或方法就会被隐藏(可以理解为重写),如果还想要使用父类中的这个成员变量或方法,就需要用到super. ...
- 要生成在[min,max]之间的随机整数,
import java.util.Random; public class RandomTest { public static void main(String[] args) { int max= ...