【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 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的更多相关文章
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【数组】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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- ntpath join(path, *paths) 发生UnicodeDecodeError的Bug的解决方案
最近在学习用Python开发web,使用的是web框架,在win8.1平台下安装SAE和Flask的时候总是在ntpath.py这个UnicodeDecodeError模块报错. 84行 result ...
- .Net Remoting浅释
面试的时候,被问到.Net Remoting 工作方式及它和Web Service的区别,在此做个整理,顺便回顾一下. .Net Remoteing: 我们可以认为.Net Remoting是一种分布 ...
- 用三或四个个div标签实现工字效果
使用重构的方式制作出一个如下图的水平.垂直都居中,短边为50px,长边为150px的红色“工”字. a) 使用3个div完成 <!DOCTYPE html><html lang=&q ...
- Struts_json插件配置参数
Struts中使用json需要在struts基础上加上几个包:(这里只列出了重要的几个) commons-lang-2.4.jar: jsonplugin-0[1].32.jar: 下面是配置文件中的 ...
- node c#
blogs.msdn.com/b/brunoterkaly/archive/2012/02/22/node-js-socket-programming-with-c-and-javascript.as ...
- RH的NFS配置--简单OK
参照文档: http://wenku.baidu.com/link?url=SAcDvj8WtBd8dunC7P6FTFADYYVzzxhOiNJqbgr-aGTZovM0lHg-wbYgv9I3Lu ...
- 算术编码JM实现
h.264标准中,CABAC的算术编码部分(9.3.4)只是一个参考,实际编码器中并不一定会按照它来实现,像JM中就有自己的算术编码实现方案. 在上篇文章CABAC中有详细的算术编码描述,在了解算术编 ...
- mysql grant 示例
' with grant option; FLUSH PRIVILEGES;
- uva 11731 - Ex-circles
题意:已知三角形ABC的3条边长,求三角形ABC 的面积,以及阴影部分的总面积. #include<iostream> #include<cstdio> #include< ...
- Xmanager连接CentOS的远程桌面
本文主要介绍通过Xmanager连接CentOS远程桌面时,在CentOS系统上需要做的一些配置. 1. Xmanager简介 Xmanager是一个运行于 Windows平台上的高性能的X Serv ...