121. 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.

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output: 5
 
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1]
Output: 0
 
In this case, no transaction is done, i.e. max profit = 0.

我的思路:n*n的时间复杂度,从前向后,每次找出后面比当前数小的数求差,然后和最大差比较后更新最大差。

思路2:第i天卖出的收益为第i天的股价与前面i-1天中最小股价的差。从前向后遍历的过程中,记录当前最小的股价和第i天的收益。然后将第i天的收益和全局最大收益作对比以决定是否更新全局最大收益。

30. leetcode 121. Best Time to Buy and Sell Stock的更多相关文章

  1. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  2. [LeetCode] 121. 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 ...

  3. LeetCode 121. 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 ...

  4. Java for LeetCode 121 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 ...

  5. leetcode 121. Best Time to Buy and Sell Stock ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  6. Python [Leetcode 121]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 ...

  7. [leetcode]121. 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 ...

  8. Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)

    题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...

  9. LeetCode 121. Best Time to Buy and Sell Stock (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

随机推荐

  1. CSS技巧和经验列表

    如何清除图片下方出现几像素的空白间隙? img{display:block;} 如何让文本垂直对齐文本输入框? input{vertical-align:middle;} 如何使文本溢出边界显示为省略 ...

  2. Java学习笔记--动态代理

    动态代理 1.JDK动态代理 JDK1.3之后,Java提供了动态代理的技术,允许开发者在运行期创建接口的代理实例.JDK的动态代理主要涉及到java.lang.reflect包中的两个类:Proxy ...

  3. ASP.NET Core部署到CentOS7,使用Nginx代理

    ASP.NET Core 的运行环境由新开发的 Kestrel Server 负责,IIS 退回到 HTTP 的侦听器的角色,微软也特别为了这个需求开发了 IIS Platform Handler,以 ...

  4. perl 祖先类UNIVERSAL

    在perl 面向对象编程里,同其它语言一样存在祖先类.所有类默认继承UNIVERSAL的属性和方法. UNIVERSAL​类有几个常用方法can,isa. can可以检查一个对象是否有相应的方法,这个 ...

  5. 0.搭建myeclipse开发环境

    1.配置jdk 2.myeclipse集成tomcat 选择默认jdk

  6. JavaWeb 后端 <十> 之 数据池 C3P0 DPCB JNDI

    一.数据库连接池原理:(理解) //模拟数据库连接池的原理 public class ConnectionPoolDemo { private static List<Connection> ...

  7. gulp环境搭建

    简介: gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:它不仅对网站资源进行优化,而且在开发过程中很多重复的任务,他可以通过明确的工具自动完成,使用它我们不仅可以很愉快的编写代码 ...

  8. org.apache.jasper.JasperException: - Page directive must not have multiple occurrences of pageencoding

    最近写jsp遇到一系列的低级错误,记录下来权当前车之鉴吧. 错误提示: SEVERE: Servlet.service() for servlet jsp threw exceptionorg.apa ...

  9. easyui-dialog里面的东西

    <div id="id_open" class="easyui-dialog" title="公司简介" style="wi ...

  10. java基础03 位运算符

    位运算是对整数的二进制位进行相关操作,详细运算如下: 非位运算值表 A ~A 1 0 0 1 与位运算值表 A B A&B 1 1 1 1 0 0 0 1 0 0 0 0 或位运算值表 A B ...