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. Dalvik虚拟机进程和线程的创建过程分析

    从前面Dalvik虚拟机的运行过程分析一文可以知道,Dalvik虚拟机除了可以执行Java代码之外,还可以执行Native代码,也就是C/C++函数. 这些C/C++函数在执行的过程中,又可以通过本地 ...

  2. X/Open DTP——分布式事务模型

    转载:http://www.cnblogs.com/aigongsi/archive/2012/10/11/2718313.html 这一几天一直在回顾事务相关的知识,也准备把以前了解皮毛的知识进行一 ...

  3. 2015_WEB页面前端工程师_远程测题_东方蜘蛛_1

    请使用HTML+CSS实现如下效果: 1. 使用CSS Sprites,实现如图1效果,素材图片为: icons.png: 2. 使用脚本语言验证邮箱.密码的必填以及邮箱的合法性: 若验证失败,则出现 ...

  4. "Cannot convert value '0000-00-00' from column 2 to TIMESTAMP"mysql时间转换bug

    今天在项目中遇到这样的一个bug,Cannot convert value '0000-00-00' from column 2 to TIMESTAMP 仔细一查,经过http://blog.csd ...

  5. hadoop Yarn 编程API

    客户端编程库: 所在jar包: org.apache.hadoop.yarn.client.YarnClient 使用方法: 1 定义一个YarnClient实例: private YarnClien ...

  6. Python 计算已经过去多少个周末

    def weekends_between(d1,d2): days_between = (d2-d1).days weekends, leftover = divmod(days_between,7) ...

  7. Hibernate 使用注解后没发现建表

    可能的原因: ①该注解类中的注解出错:例如是否因为属性名与数据库关键字冲突 ②是否添加包扫描配置: <property name="packagesToScan" value ...

  8. Android 两个Activity进行数据传送 发送

    Activity1:: Intent intent= new Intent(this, OtherActivity.class); String name = "heyiyong" ...

  9. win7电脑自动关机怎么设置

    WIN7系统自带了关机工具的,下面是步骤 1.“开始”-右键点击“计算机”选择“管理”,在左侧界面中选择“任务计划程序”. 2.在右侧界面中选择“创建基本任务”(向导式创建任务,推荐新手使用)或者“创 ...

  10. ArtisticStyle----很好用的C/C++样式格式化工具

    下载地址:http://srgb.googlecode.com/files/AStyle_2.02_windows.7z 把astyle.exe 复制到 C:\WINDOWS 目录里,省的指定路径VC ...