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. 零售ERP开发(一)

    随着企业信息化不断提高,各种企业管理系统应用而生:怎么才能开发一套简洁.易用.功能健全的管理系统,尤其是能够帮助企业做出重大策略的系统,一致是我们追求的目标.近些年,接触的Erp系统主要包括国内比较知 ...

  2. 浅析tornado web框架

    tornado简介 1.tornado概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Py ...

  3. css 多出一行或多行后显示...的方法

    一行超出显示... .mui-ellipsis { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } 两行超出的显示. ...

  4. C 字符串倒转,XCode中编译

    正在学习ios开发,在前期学习c时,常规方法直接倒转数组的值,只能用于非中文字符,否则出现乱码, 在网上找了点资料,可能是 IDE不一致,一直得不到自己想要的值.花时间自己改了一下,正常通过 //字符 ...

  5. c#中的整形类型

    一.整型类型 C#中定义了8中整数类型:字节型(byte).无符号字节型(ubyte).短整型(short).无符号短整型(ushort).整型(int).无 符号整型(uint).长整型(long) ...

  6. 金山网络2014春季Android实习生招聘-成都站-笔试第一题

    实现单例模式,并实现方法int getResult(float a),将a*8后返回. package jinshanwangluo.exam; /** * @author guoxm * @date ...

  7. 使用spring的jdbcTemplate-----用JDBC模板查询数据库

    JdbcTemplate类声明了几个重载的query()模板方法来控制整个查询过程,就像进行更新数据操作一样,通过实现PreparedStatementCreator和PreparedStatemen ...

  8. 网页错误404 or 500

    HTTP 错误 400 400 请求出错 由于语法格式有误,服务器无法理解此请求.不作修改,客户程序就无法重复此请求. HTTP 错误 401 401.1 未授权:登录失败 此错误表明传输给服务器的证 ...

  9. [BZOJ 1112] [POI2008] 砖块Klo 【区间K大】

    题目链接:BZOJ - 1112 题目分析 枚举每一个长度为k的连续区间,求出这个区间的最优答案,更新全局答案. 可以发现,这个区间的所有柱子最终都变成这k个数的中位数时最优,那么我们就需要查询这个区 ...

  10. 全球顶级专家为你解读:什么是真正的 DevOps?

    [编者按]本文是 Skytap 内容主编 Noel Wurst 对 DevOps Enterprise Summit (DOES)的不完全综述,内容包括了 Noel 和一些与会嘉宾的思考,旨在勾画 D ...