【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 ...
随机推荐
- 20 Valid Parentheses(匹配括号)
题目意思:判断一个字符串(){}[]是否符合 思路:用栈ps:实习一个多月了,代码也刷不动了,状态真不是一般的差 class Solution { public: bool isValid(strin ...
- javascript动态添加效果
<script type="text/javascript"> window.onload=function(){ $("#ch").click(f ...
- python【第十四篇】HTML与CSS初遇
概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...
- Day17 表单验证、滚动菜单、WEB框架
一.表单验证的两种实现方式 1.DOM绑定 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- java 正则操作之获取
// 正则操作 获取import java.util.regex.*;class Demo{ public static void main(String[] args){ String str=& ...
- 学习总结之Log4NET
通过在网上查找了一些资料,用了些时间学习了log4NET,做了一个小小的总结,说一下它的特点吧 首先呢log4NET是.Net下一个非常优秀的开源日志记录组件.它可以将日志分成不同等级,也可以按照我们 ...
- Junit 源码剖析(一)
采用Junit4.8.2分析Junit实现架构 源码架构两个大包:junit包 org包 首先分析org.junit.runners.model包下的几个类 org.junit.runners.mod ...
- BCB 语言类
整理日: 2015年2月16日 EcLanguage.h /*--------------------------------------------------------------------- ...
- sqlserver 字符串处理函数解释
1.ASCII()返回字符表达式最左端字符的ASCII 码值.在ASCII()函数中,纯数字的字符串可不用‘’括起来,但含其它字符的字符串必须用‘’括起来使用,否则会出错.2.CHAR()将ASCII ...
- bzoj 2331: [SCOI2011]地板 插头DP
2331: [SCOI2011]地板 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 541 Solved: 239[Submit][Status] D ...