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.

code : 朴素的算法,对每一个i ,计算 最大 的 prices[j] (j>i) 来维护最大的差值,但是这样的复杂度是O(n^2),会TLE

考虑下面的O(n)算法:

class Solution {
public:
int maxProfit(vector<int> &prices) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int maxsum = 0;
if(prices.size() <= 1)
return 0; int maxPrice = prices.back();
for(int i = prices.size()-1; i >= 0; i--)
{
maxPrice = max(maxPrice,prices[i]);
maxsum = max(maxsum,maxPrice-prices[i]);
}
return maxsum ; }
};

【LeetCode】Best Time to Buy and Sell Stock的更多相关文章

  1. 【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 ...

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

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

  3. 【leetcode】Best Time to Buy and Sell Stock II

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

  4. 【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 ...

  5. 【数组】Best Time to Buy and Sell Stock I/II

    Best Time to Buy and Sell Stock I 题目: Say you have an array for which the ith element is the price o ...

  6. 【leetcode】Best Time to Buy and Sell 3 (hard) 自己做出来了 但别人的更好

    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 2(too easy)

    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 (easy)

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

  9. 【Leetcode】【Medium】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 ...

随机推荐

  1. node初步二 小爬虫

    小爬拉勾网 获取想要的信息: 一.分三步 1 获得数据 2 处理数据 3展示数据 二 .代码 :创建文件reptile.js;写入 var http=require('http'); var chee ...

  2. android sdk api的层次结构

    一.系统级:android.accounts android.app     1.OS 相关         android.os         android.os.storage         ...

  3. Android Linux自带iptables配置IP访问规则

    利用Linux自带iptables配置IP访问规则,即可做到防火墙效果

  4. sizeof()用法

    参考:sizeof_百度百科 sizeof()用法汇总(经典) 声明:本文是笔者抽出对自己有用的细节,对前两文的总结. 1.sizeof概念 sizeof是C语言中判断数据类型或者表达式长度符:不是一 ...

  5. WebApi学习总结系列第五篇(消息处理管道)

    引言: ASP.NET WebAPI的核心框架是一个消息处理管道,这个管道是一组HttpMessageHandler的有序组合.这是一个双工管道,请求消息从一端流入并依次经过所有HttpMessage ...

  6. Word神器使用

    调整自动换行符的换行起始点: 拖拽下面的三角部分(三角下面的四边形部分不要碰),就可以调整自动编号的换行后的其实点在哪里.

  7. (bug更正)利用KVC和associative特性在NSObject中存储键值

    KVC 一直没仔细看过KVC的用法,想当然的认为可以在NSObject对象中存入任意键值对,结果使用时碰到问题了. 一个简单的位移动画: CAKeyframeAnimation *keyPosi=[C ...

  8. android学习(2) 多线程的理解

    多线程操作UI的运行原理: UI线程:首先启动app时,系统会自动启动一个UI线程,然后此线程会创建一个Looper(注:Looper构造函数会实例化一个MessageQueue的消息队列存在变量mQ ...

  9. System.Reflection.Assembly.GetEntryAssembly()获取的为当前已加载的程序集

    今天在使用System.Reflection.Assembly.GetEntryAssembly()获取程序集时,发现获取的程序集不全.原来是因为C#的程序集为延迟加载,此方法只获取当前已加载的,未加 ...

  10. 李洪强漫谈iOS开发[C语言-033]-三元运算符的应用