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.

 从右到左扫描,维护一个当前节点右边最低的股价,用当前股价和最低股价的差作为最大收益交易的候选。扫描一遍,即可得最大收益。

class Solution {
public:
//I
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = prices.size();
if(len < ) return ; int min = prices[];
int res = ;
for(int i = ; i< prices.size(); ++i)
{
if(prices[i] < min){
min = prices[i] ;
continue;
}
res = res > prices[i] - min ? res : prices[i] - min ;
} return res;
}
};

Leetcode_Best Time to Buy and Sell Stock的更多相关文章

  1. LeetCode_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 al ...

  2. 【leetcode】121-Best Time to Buy and Sell Stock

    problem 121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<i ...

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

  4. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

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

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

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

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

随机推荐

  1. js 删除效果代码

    效果 css文件delcss.css 代码如下: ;;} #div1{;; filter:alpha(opacity:0); display:none;} #div1 h3{ height:20px; ...

  2. 2014-08-01 ASP.NET中对SQLite数据库的操作——ADO.NET

    今天是在吾索实习的第18天.我主要学习了如何在ASP.NET中对SQLite数据库的操作,其基本操作如下: 添加引用System.Data.SQLite.dll(PS:在网页里面任意找到适合的.NET ...

  3. HDU_2028——求多个数的最小公倍数

    Problem Description 求n个数的最小公倍数.   Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数.   Output 为每组测试数据输出它们的最 ...

  4. javascript中this、apply、call、bind的用法和区别

    参考阮一峰文章链接:http://javascript.ruanyifeng.com/oop/basic.html#toc10

  5. [深入React] 9.小技巧

    如果组件没有state那就可以精简成一个render function var Item = function(props){ return _('div',null,'名称:'+props.name ...

  6. referer报头

    依据客户的到达方式定制页面 referer报头保存了用户是从哪个网站找到到这里来的信息的.如果用户直接输入了页面的地址,浏览器就不会发送referer信息. 5.6.1 创建一个可以根据链接过来的网站 ...

  7. 高速排序-c++(分别用数组和容器实现)

    /********************************************************************** *版权全部 (C)2014, cheng yang. * ...

  8. uva 10626 - Buying Coke(记忆化搜索)

    题目链接:10626 - Buying Coke 题目大意:给出要买可乐的数量, 以及1元,5元和10元硬币的数量, 每瓶可乐8元,每次照钱会按照最少硬币的方式找回, 问如何投币可使得投入的硬币数最少 ...

  9. Linux编程环境介绍(2) -- shell(Bash) 介绍

    1. 在计算机科学中,Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器).它类似于DOS下的command和后来的cmd.exe. 2. bash (Bourne Aga ...

  10. mvc之验证IEnumerable<T> 类型

    假设我们有这么一种需求,我们要同时添加年级和年级下面的多个班级,我们一般会像下面这种做法. Action中我们这样接收: [HttpPost] public ActionResult CreateGr ...