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. redis相关目录

    redis的docker化安装 redis的主从配置

  2. Arduino平台基于DbC的软件调试

    基于LED和串口通信的DBC调试工具:HAssert --- Hyper LED/Serial Assert . 本文基于DbC思想 ,在Arduino平台上实现了两种断言显示方式---LED显示和串 ...

  3. Ubuntu装完后要做的几件事

    Ubuntu装完后要做的几件事 改hosts 无论哪里,改hosts都是第一件事,没hosts咋google.没google咋活.在终端输入命令 sudo gedit /etc/hosts在# The ...

  4. 第三章 最简单的C程序设计——顺序程序设计

    一.数据的表现形式及其运算 1.常量和变量 在计算机高级语言中,数据有两种表现形式:常量和变量. 1.1.常量 在程序运行过程中,其值不能被改变的量称为常量.如:5,6,32,0.111. 数值常量就 ...

  5. "Mon Dec 31 00:00:00 CST 2012" java日期装换 "yyyy-MM-dd"

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import ja ...

  6. 11 TCP实现QQ聊天

    1.客户端参考代码 #coding=utf-8 from socket import * # 创建socket tcpClientSocket = socket(AF_INET, SOCK_STREA ...

  7. pprof 查看goroutine

    package main import ( "net/http" "runtime/pprof" ) var quit chan struct{} = make ...

  8. vux用法

    其实官网写的很详细了 但是好多时候没有仔细看的耐心 下面基本也是vux官网步骤: 很多人需要$t未定义问题 其实按着官网来就能解决这个报错: 如果你遇到 $t 报错问题,请不要开 issue,升级 v ...

  9. 怎样安装PyCharm

    在地址栏输入http://www.jetbrains.com/pycharm/ 打开PyCharm官网 http://idea.lanyus.com/

  10. window平台下使用python虚拟环境

    第一步:安装virtualenv模块 安装virtualenv模块,使用pip install C:\Users\wangjun>pip install virtualenv 第二步:创建虚拟环 ...