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.

eg:输入为:[6,4,3,7,3,8,1,4,7,6],则最大收益为7-1=6。

思路一:

先找到数组中的最大数8,在第5天以8卖出时,必须在第0-5天买入,且买入时的价格要为第0-5天的最低价3,此时的收益为8-3=5;如果第0-5天的最低价3并不是整个数组的最低价,

再考虑在第5天之后进行买卖交易,找到第6-9天的最高价7(第8天卖出),第6-8天的最低价1(第6天买入),此时的收益为7-1=6;若要在第8天之后进行买卖交易,则收益为0。所以

最大收益为max(5,6,0),即最大收益为6。

 class Solution {
public:
int maxProfit(vector<int> &prices) {
int max_profit=, buy_pos, sell_pos;
vector<int>::iterator it1=prices.begin();
vector<int>::iterator it2=prices.end();
while(it1<it2)
{
vector<int>::iterator sell_it = max_element(it1, it2);
vector<int>::iterator buy_it = min_element(it1, sell_it+);
if(max_profit<(*sell_it-*buy_it))
max_profit = *sell_it-*buy_it;
it1 = sell_it+;
}
return max_profit;
}
};

但是这种方法复杂度高,会出现Time limit exceed。

思路二:

eg:输入为:[6,4,3,7,3,8,1,4,7,6]

分别考虑如果在第0天,第1天,第2天,...,第9天卖出股票时的收益,这9种情况下的收益最大值就是我们要求的最大收益。

要在第i天卖出股票,则必须在第0~i天中的某一天买入,当然选取第0~i天内价格最小的一天买入,记这个最小值为buyPrice_i,则第i天卖出股票时的收益为prices[i]-buyPrice_i;

在求buyPrice_i时可以动态地求,不需要每次从头到i遍历一遍。最后我们要求的maxprofit=max(prices[0]-buyPrice_0, prices[1]-buyPrice_1, ... , prices[9]-buyPrice_9)。

 class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.size()==)
return ;
int max_profit=, sell_pos;
int min_buy_price=prices[];
for(sell_pos=; sell_pos<prices.size(); sell_pos++) //在sell_pos卖出的话,必须在0-sell_pos之间买入,所以sell_pos当天卖出时的最大收益要求buy_pos在0-sell_pos之间最小
{
if(min_buy_price>prices[sell_pos])
min_buy_price = prices[sell_pos];
if(max_profit<(prices[sell_pos]-min_buy_price))
max_profit = prices[sell_pos]-min_buy_price;
}
return max_profit;
}
};

这种思路可以accept。

[LeetCode OJ] Best Time to Buy and Sell Stock I的更多相关文章

  1. LeetCode OJ - Best Time to Buy and Sell Stock

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/32939749 Say you have ...

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

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

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

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

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

  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 Week6]Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...

随机推荐

  1. 2015第42周六Pgsql全文索引

    全文搜索通常也就是文本搜索,它可以提供满足查询的识别自然语言的能力,并且任意性地通过相关性查询进行排序.搜索最常见的类型就是找到所有包含给定的查询术语的记录,并且以相似性的查询顺序返回它们. 对于普通 ...

  2. 嵌入式系统烧写uboot/bootloader/kernel的一般方法

    嵌入式系统烧写uboot/bootloader/kernel的一般方法   本文介绍了在嵌入式系统中烧写uboot/bootloader/kernel 的一般方法,以及如果uboot或者内核出现错误, ...

  3. Linux学习笔记20——第一个多线程程序

    一 什么是线程 线程:是一个进程内部的一个控制序列. 二 使用POSIX的注意点 1 为了使用线程函数库,必须定义宏_REENTRANT,通过定义_REENTRANT来告诉编译器我们需要可重入功能,可 ...

  4. UnderStand Perspective Rasterization, SV_POSITION(gl_FragCoord) to Pixel, SV mean Systems Value

    Shader "UnderStandPRR" { Properties { _MainTex ("Texture", 2D) = "white&quo ...

  5. JavaScript DOM-Ready 机制

    IE9开始和其他现代浏览器可以通过绑定DOMContentLoaded事件:IE9之前的的浏览器需要绑定onreadystatechange事件并等待readyState为"complete ...

  6. 让x86的android模拟器能模拟arm架构系统

    网上介绍共计三种模拟器比较常用,分别是bluestacks.andy和Genymotion,前者支持ARM架构,中者支持远程控制,后者启动速度快,各有优缺点. 如果要用genymotion模拟arm的 ...

  7. weblogic 日志介绍

    1.server.log 该日志记录的是服务(包括admin server 和 app server)启动过程中和关闭过程中的日志,还包括部署在服务上面的应用,在运行过程中所产生的日志. server ...

  8. 【Java】集合_Collections_学习记录

    一.Collections工具类概述 1.为List.Set.Map等集合提供大量方法对集合元素进行排序.查询和修改等操作. 2.将集合对象设置为不可变. 3.对集合对象实现同步控制等. 二.排序操作 ...

  9. uva1620 Lazy Susan

    留坑(p.256) 什么找规律啊 坑爹 #include<cstdio> #include<cstring> #include<cstdlib> #include& ...

  10. 20169210《Linux内核原理与分析》第三周作业

    本次作业也是分为两部分,第一部分是对实验楼<Linux基础入门>复习,第二部分为对课本18章的复习. 第一次学习实验楼的<Linux基础入门>时由于是第一次接触Linux,所以 ...