前言

 

【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;
}
};
作者:Double_Win
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则作者保留追究法律责任的权利。  若本文对你有所帮助,您的关注推荐是我们分享知识的动力!

[LeetCode 题解]:Best Time to Buy and Sell Stock的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. 【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 ...

  9. [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 ...

  10. 【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 ...

随机推荐

  1. python学习——练习题(9)

    """ 题目:暂停一秒输出. 程序分析:使用 time 模块的 sleep() 函数. http://www.runoob.com/python/python-date- ...

  2. 【转】RocketMQ事务消费和顺序消费详解

    RocketMQ事务消费和顺序消费详解 转载说明:该文章纯转载,若有侵权或给原作者造成不便望告知,仅供学习参考. 一.RocketMq有3中消息类型 1.普通消费 2. 顺序消费 3.事务消费 顺序消 ...

  3. springboot 中使用thymeleaf

    Spring Boot支持FreeMarker.Groovy.Thymeleaf和Mustache四种模板解析引擎,官方推荐使用Thymeleaf. spring-boot-starter-thyme ...

  4. 如何在windows下安装mongoDB扩展

    安装环境   系统环境:Windows 10 64位   Apache版本:2.4.9   PHP版本:5.5.12   MongoDB版本:3.2.6   Wamp版本:wamp 2.5 86位   ...

  5. UISprite

    [UISprite] UISprite用于引用一个UIAtlas中的sprite. 此脚本最强大的功能是Sprite Type,主要指定在需拉伸图片时的行为,它有以下几种值: 1)Simple:普通拉 ...

  6. Excel VBA入门(七)注释、宏按钮及错误处理

    系统性的知识前面已经讲完,从本章开始,本系列教程涉及的将会是一些相对凌散的内容. 1. 注释 代码注释是一件利人利己的事,为了方便自己在代码需要更新修改时,依然能够快速地看懂自己完的每一行代码到底是什 ...

  7. 五分钟带你入门TensorFlow

    TensorFlow是Google开源的一款人工智能学习系统.为什么叫这个名字呢?Tensor的意思是张量,代表N维数组:Flow的意思是流,代表基于数据流图的计算.把N维数字从流图的一端流动到另一端 ...

  8. Scrapy Test

    (flappbird) luo@luo-ThinkPad-W540:~$ scrapy startproject myspider0315New Scrapy project 'myspider031 ...

  9. linux操作系统下,怎么使用kill按照PID一次杀死多个进程

    1.ps -ef | grep firefox | grep -v grep | cut -c 9-15 | xargs kill -s 9 说明:“grep firefox”的输出结果是,所有含有关 ...

  10. java的static块及相关内容

    原文地址:http://blog.csdn.NET/lubiaopan/article/details/4802430     感谢原作者! static{}(即static块),会在类被加载的时候执 ...