【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 ...
随机推荐
- sass用法
可能刚开始我们学习前端的时候都习惯用html+css.来做网页,但是我们发现css有很多重复的代码或者是你要改里面的图片或者文字还有去诶个的找很麻烦,所以我们就用sass来简化它. 首先我们需要安装一 ...
- Activity的"singleTask"之谜
官方文档称 以这种方式启动的Activity总是属于一个任务的根Activity.果真如此吗?本文将为你解开Activity的"singleTask"之谜. 任务(Task)是个什 ...
- python中的字典应用实例
字典中的键使用时必须满足一下两个条件: 1.每个键只能对应一个项,也就是说,一键对应多个值时不允许的(列表.元组和其他字典的容器对象除外).当有键发生冲突时(即字典键重复赋值),取最后的赋值. > ...
- 基于python做的抓图程序1.0.00版本
#coding=gbkimport urllibimport urllib2import reimport osimport time# import readline def getHtml(url ...
- iOS: 学习笔记, 用代码驱动自动布局实例(swift)
iOS自动布局是设置iOS界面的利器.本实例展示了如何使用自动布局语言设置水平布局, 垂直布局1. 创建空白iOS项目(swift)2. 添加一个控制器类, 修改YYAppDelegate.swift ...
- C# 下载资源
//创建一个初始化请求对象 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://wwww.b ...
- 常见HTTP状态码大全
我们经常会遇到404.500.302等提示,它们究竟是什么意思呢?除了这几个常见的状态码外,还有哪些我们没有遇到过的但有可能出现的状态码呢?网站的http状态对于网站维护人员来说是相当重要的,当网站出 ...
- jq实现瀑布流效果
<!doctype html><html><head><meta http-equiv="Content-Type" content=&q ...
- 数据结构练习 00-自测4. Have Fun with Numbers (20)
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- http server v0.1_http_server.c
/**************************************************************** filename: http_server.c author: xx ...