题目

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.

分析

求买卖最大收益问题。简单来说就是给定一个整数序列,求最大差值。

该题目的关键是,效率。

我们很容易想到二次循环解决该问题,但是很不幸,是超时的! 所以,必须另择他法。

其实在O(n)时间内,也可以解决问题,只需要加一个记录当前最低价格的变量即可;价低购入,价高售出嘛~

AC代码

class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty())
return 0; //maximum记录最大收益,price记录当前最低价格
int maximum = 0, price = prices[0] ,size = prices.size(); for (int i = 1; i < size ; ++i)
{
//价低时买入
if (prices[i] < price)
{
price = prices[i];
continue;
}
//价高时卖出,比较并记录最大收益
else{
int tmp = prices[i] - price;
if (tmp > maximum)
maximum = tmp;
}
}//for
return maximum;
}
};

GitHub测试程序源码

LeetCode(121) Best Time to Buy and Sell Stock的更多相关文章

  1. LeetCode(122) 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 ...

  2. LeetCode(123) Best Time to Buy and Sell Stock III

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  3. LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV

    Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...

  4. &lt;LeetCode OJ&gt; 121. /122. Best Time to Buy and Sell Stock(I / II)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  5. LeetCode(121):买卖股票的最佳时机

    Easy! 题目描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买 ...

  6. LeetCode Array Easy 122. Best Time to Buy and Sell Stock II

    Description Say you have an array for which the ith element is the price of a given stock on day i. ...

  7. [Leetcode 122]买股票II 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 ...

  8. [LeetCode&Python] Problem 122. 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 (贪心策略 or 动态规划)

    Best Time to Buy and Sell Stock Total Accepted: 14044 Total Submissions: 45572My Submissions Say you ...

随机推荐

  1. C# 面向对象之面向接口

    接口的定义 与类不同的是接口用interface关键字 (1)接口中所有成员不能添加任何修饰符,默认为public,如果显示指定修饰符将会出现编译错误; (2)接口中不能包含字段.运算符重载.实例构造 ...

  2. UWP 保存用户设置

    一:需求 需要保存用户设置,用户下一次再打开app时,加载默认的设置.比如用户设置的主题颜色,用户自定义的文件保存路径等. 一般应用的的数据存储分为两种,一种是云存储(将数据保存在云端,下次打开的时候 ...

  3. 牛客小白月赛13 E(图、矩阵幂)

    AC通道 如果建立第一天某点到某点有几条路的矩阵,做k次矩阵乘就是第k天某点到某点有几条路.统计即可. #include <bits/stdc++.h> using namespace s ...

  4. [aspnetcore]asp.net core程序部署到Ubuntu中的路径问题

    先标记下正确写法 new FileInfo(Environment.CurrentDirectory + "/Config/Log4net.config") 很多同行喜欢这样写: ...

  5. 084 Largest Rectangle in Histogram 柱状图中最大的矩形

    给出 n 个非负整数来表示柱状图的各个柱子的高度,每个柱子紧挨彼此,且宽度为 1 .您的函数要能够求出该柱状图中,能勾勒出来的最大矩形的面积. 详见:https://leetcode.com/prob ...

  6. 头部和信号栏一个颜色appcloud

    <header id="header" > <ul > <li class="active" onclick="api. ...

  7. java设计模式--建造模式

    建造模式 建造模式属于对象创建型模式,建造模式的目的为将复杂对象的构建过程与其部件实现方式分离,使得同样的构建过程可以有不同的表示,同时相同的构建过程也能够适用于不同的部件实现方式. 建造模式的适用性 ...

  8. 【Android】SlidingMenu属性详解

    SlidingMenu 常用属性介绍: menu.setMode(SlidingMenu.LEFT);//设置左滑菜单menu.setTouchModeAbove(SlidingMenu.TOUCHM ...

  9. Jenkins默认工作空间及更改默认工作空间

    1.Jenkins安装到tomcat 需2步: ①官网下载Jenkins(一个war包) ②安装 所谓安装,也有两种形式: 一是在安装了jdk的情况下直接运行:java -jar jenkins.wa ...

  10. 在使用线程池时应特别注意对ThreadLocal的使用

    使用ThreadLocal并且有线程池时要特别注意,ThreadLocal是以线程为key的,而线程池里面的线程是会被重新利用的,所以如果有使用线程池并且使用ThreadLocal来保存状态信息时要特 ...