题目描述

已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益

测试样例

Input: [7, 1, 5, 3, 6, 4]
Output: 5
最大收益 = 6-1 = 5 (不是7-1 = 6,因为先买后卖,7买,1买亏了6) Input: [7, 6, 4, 3, 1]
Output: 0
最大收益为0

详细分析

初看非常简单,遍历数组,每次选择一个元素,找到这个元素后面的数组的最大值,计算差值,和当前最大收益比较即可,就像这样:

[7,1,5,3,6,4] 当前7,后面最大6,收益-1

[7,1,5,3,6,4] 当前1,后面最大6,收益5

[7,1,5,3,6,4] 当前5,后面最大6,收益1

如此继续找到即可。

不过这种方法会超时,稍微改变一下,现在不止保持最大收益,还保存最低价格,如果当天价格更低,就刷新最小价格(买),同时如果当天价格减去最小价格的收益最大,就刷新最大价格(卖),过程如下:

[7,1,5,3,6,4] minPrice=INT_MAX,maxProfit=0 => minPrice=7,maxProfit=0

[7,1,5,3,6,4] minPrice=7,maxProfit=0 => minPrice=1,maxProfit=0

[7,1,5,3,6,4] minPrice=1,maxProfit=0 => minPrice=1,maxProfit=4

[7,1,5,3,6,4] minPrice=1,maxProfit=4 => minPrice=1,maxProfit=4

[7,1,5,3,6,4] minPrice=1,maxProfit=4 => minPrice=1,maxProfit=5

[7,1,5,3,6,4] minPrice=1,maxProfit=5 => minPrice=1,maxProfit=5

算法实现

  • 方法1:Time Limit Exceeded
class Solution {
public:
int maxProfit(vector<int>& prices) {
int profit = 0;
for(auto start=prices.begin();start!=prices.end();start++){
int buy = *start;
int sell = *std::max_element(start,prices.end());
if((sell - buy)>profit){
profit = sell-buy;
}
}
return profit;
}
};
  • 方法2: Accepted
class Solution{
public:
int maxProfit(vector<int> &prices){
int profit = 0;
int minBuy = std::numeric_limits<int>::max();
for(int i=0;i<prices.size();i++){
//buy it if current price is less than minimum prices yet
if(prices[i]<minBuy){
minBuy = prices[i];
}
//sell it if current profit got optimally
if((prices[i]-minBuy)>profit){
profit = prices[i]-minBuy;
}
}
return profit;
}
};

Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)的更多相关文章

  1. 30. leetcode 121. Best Time to Buy and Sell Stock

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  2. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

  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. Java for 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 ...

  6. leetcode 121. Best Time to Buy and Sell Stock ----- java

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

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

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

  9. LeetCode 121. Best Time to Buy and Sell Stock (stock problem)

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

随机推荐

  1. Celery-4.1 用户指南:Testing with Celery (用 Celery测试)

    任务与单元测试 在单元测试中测试任务行为的推荐方法是用mocking. Eager mode: task_always_eager 设置启用的 eager 模式不适用于单元测试. 当使用eager模式 ...

  2. Flash 零日漏洞复现(CVE-2018-4878)

    项目地址:https://github.com/Sch01ar/CVE-2018-4878.git 影响版本为:Adobe Flash Player <= 28.0.0.137 攻击机器IP:1 ...

  3. Spring MVC F5刷新问题

    转自:https://bbs.csdn.net/topics/390771056 post操作成功后重定向到B,这样浏览器里F5的时候就不会让提交A了    

  4. MXF文件结构浅析

    MXF是英文Material eXchange Format(素材交换格式)的缩语.MXF是SMPTE(美国电影与电视工程师学会)组织定义的一种专业音视频媒体文件格式.MXF主要应用于影视行业媒体制作 ...

  5. 【知识碎片】Net项目经验积累

    后台传JSON到js报错 MVC控制器传json到前端JS"变为" 导致JS报错 重点是一定要在@ViewBag.typeJson两边加双引号,并且后台用 编码前台解码 ViewB ...

  6. 记录一次从txt文件导入数据的python下的MySQL实现

    环境: python2.7 ComsenzXP自带MySQL 安装python-MySQL模块 数据格式:txt格式的账号信息. 数据一行一条数据. 难点:有的行只有账号,没有密码:有的为空行:有的行 ...

  7. Latex 多个参考文献的引用

    如果在文章中出现连续引用多个参考文献的情况,希望显示的格式为 [1-5,9,12],那么可以如下处理: 在文章的导言区加 \usepackage[square, comma, sort&com ...

  8. Tornado之抽屉实战(2)--数据库表设计

    经过我们上次分析,数据库要有最基本的四张表,用户表,消息表,类型表,点赞表,评论表,接下来我们看着怎么设计吧 首先我们要清楚,表设计的代码是写在models下的 用户表 ? 1 2 3 4 5 6 7 ...

  9. 第一个Dockerfile

    1. 创建docker目录 $ mkdir docker && cd docker 2. 编写Dockerfile $ vim Dockerfile [docker/Dockfile] ...

  10. IE双边距bug

    标准参考 根据 W3C CSS2.1 规范中的描述,对于非替换的浮动元素,若 'margin-left' 或 'margin-right' 特性的计算值为 'auto',则它们的实际使用值为 '0'. ...