Say you have an array for which the i th 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.

题意:至多做一次买、卖,求利润的最大值。

思路:这个不是简单的求出数组的最大值和最小值,然后两者相减即可,因为,卖股票必须在买股票之前,也就是,较小值要在前。所以思路为:维护两个变量,一个是目前为止的最小值,一个是目前为止的利润。遍历数组,若当前值比最小值小,则更新最小值,若比最小值大则,计算其与最小值之间的差值是否大于当前最大利润,是,更新利润。代码如下:

 class Solution {
public:
int maxProfit(vector<int> &prices)
{
int minVal=prices[];
int profit=; for(int i=;i<prices.size();++i)
{
minVal=min(prices[i],minVal);
profit=max(profit,prices[i]-minVal);
}
return profit;
}
};

[Leetcode] Best time to buy and sell stock 买卖股票的最佳时机的更多相关文章

  1. LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)

    题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不 ...

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

  3. 121. Best Time to Buy and Sell Stock 买卖股票的最佳时机

    网址:https://leetcode.com/problems/Best-Time-to-Buy-and-Sell-Stock/ 第一想法是滑动窗口法,稍微尝试后发现不可行,至少我不会... 而后想 ...

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

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

  6. 121. Best Time to Buy and Sell Stock买卖股票12

    一 [抄题]: If you were only permitted to complete at most one transaction (ie, buy one and sell one sha ...

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

  8. [LeetCode] 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 al ...

  9. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

随机推荐

  1. SpringMVC+Mybatis框架搭建

    一.新建javaweb项目,并建好相应的包结构 二.添加项目jar到lib目录下 三.在config包中新建配置文件 sping-mvc.xml,内容如下: <?xml version=&quo ...

  2. mysql日志管理#慢日志详解

    MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中 long_q ...

  3. Vue 服务器端渲染(一)

    什么是服务器端渲染(SSR)? Vue.js 是构建客户端应用程序的框架.默认情况下,可以在浏览器中输出 Vue 组件,进行生成 DOM 和操作 DOM.然而,也可以将同一个组件渲染为服务器端的 HT ...

  4. python正则表达式+正则大量实例

    正则表达式 正则表达式内部函数详解http://www.runoob.com/python/python-reg-expressions.html 正则表达式是一个特殊的字符序列,它能帮助你方便的检查 ...

  5. ctf题目writeup(2)

    2019.1.29 题目地址: https://www.ichunqiu.com/battalion 1. 点开链接: include "flag.php";$a = @$_REQ ...

  6. 【jQuery】 js 对象

    [jQuery] js 对象 一.  创建对象的三种方式 <script> var v1 = new Object(); v1.name = "name1"; v1.a ...

  7. [电子书] 《Android编程兵书》PDF

    Android编程兵书 内容简介: 这是一本Android开发书籍,内容讲解详细,例子丰富,能帮助读者举一反三.在<Android编程兵书>中,每一个知识点的描述都非常详细,并且每一个知识 ...

  8. spring多个定时任务quartz配置

    spring多个定时任务quartz配置 <?xml version=”1.0″ encoding=”UTF-8″?> <beans xmlns=”http://www.spring ...

  9. LightGBM详细用法--机器学习算法--周振洋

    LightGBM算法总结 2018年08月21日 18:39:47 Ghost_Hzp 阅读数:2360 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.ne ...

  10. kaldi GMM模型解码指令 gmm-latgen-faster详解

    目录 - 作用: - 用法: - 可选项及含义: - 使用实例: - 作用: Generate lattices using GMM-based model. 生成基于GMM模型的lattice词格) ...