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. 创建一个基本的Windows应用程序

    以下是包含的头文件 #define WIN32_LEAN_AND_MEAN // 指示编译器不要包含我们并不需要的MFC内容 #include <windows.h> // 包含所有的Wi ...

  2. [svn] 数据库操作残留,无法进行操作的解决方法

    WINDOWS环境下的解决方法: 1: 下载sqlite3数据库工具,放置于SVN的同级目录 2: CMD路径转移到Sqlite3目录 3: 残留操作选择: sqlite3 .svn/wc.db &q ...

  3. galera mysql 多主复制启动顺序及命令

    Mysql 被复制机 sql启动

  4. wiki 的捐款呼吁有感而发

    今早在维基百科上查东西时,网页的最上角出现了一栏小广告,我一看居然是维基创始人发的捐款呼吁,点开后网页内容如下: “     来自维基百科创办者Jimmy Wales的个人呼吁 ----------- ...

  5. [RxJS] Filtering operators: distinct and distinctUntilChanged

    Operator distinct() and its variants are an important type of Filtering operator. This lessons shows ...

  6. android 35 ListView增删改差

    MainActivity package com.sxt.day05_11; import java.util.ArrayList; import java.util.List; import and ...

  7. android 26 设置项目有多个入口Activity。

    第一个activity package com.sxt.day04_11; import android.os.Bundle; import android.app.Activity; import ...

  8. dump_stack的简单使用 +CALL TREE

    http://blog.chinaunix.net/uid-26403844-id-3361770.html http://blog.csdn.net/zifeng274059226/article/ ...

  9. Java语言基础(七)

    Java语言基础(七) 今天在公司多呆了会,回来晚了 一.自动类型转换 在Java中,数据是可以转换的  例如可以将byte类型的赋给int类型的 这里希望你了解内存的变化,例如 在这里,我想你应该知 ...

  10. Winform 程序中dll程序集嵌入exe可执行文件

    关于这方面,Google一下有很多方法,参考: http://blog.csdn.net/astonqa/article/details/7300856 但按照以上的方法我并没有成功,于是继续找到了一 ...