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.

暴力超时:

 class Solution:
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
n = len(prices)
if n==0:
return 0
max_diff = 0
for i in range(n):
for j in range(i,n):
diff = prices[j]-prices[i]
if diff>max_diff:
max_diff = diff return max_diff

只需要找出最大的差值即可,即 max(prices[j] – prices[i]) ,i < j。一次遍历即可,在遍历的时间用遍历low记录 prices[o….i] 中的最小值,就是当前为止的最低售价,时间复杂度为 O(n)。

class Solution:
def maxProfit(self, a):
"""
:type prices: List[int]
:rtype: int
"""
n = len(a)
if n==0:
return 0
mins = a[0]
max_diff = 0
for i in range(1,n):
#买入价也可以看成是卖出价 #找到到截止到第i天的最低买入价
if(a[i]<mins):
mins = a[i]
# 更新最大收益
elif max_diff < a[i] - mins:
max_diff = a[i] - mins
return max_diff

121. Best Time to Buy and Sell Stock(股票最大收益)的更多相关文章

  1. 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  2. 30. leetcode 121. Best Time to Buy and Sell Stock

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  3. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  4. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  5. 121. Best Time to Buy and Sell Stock@python

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

  6. [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 ...

  7. 【刷题-LeetCode】121 Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

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

  9. (Array)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 ...

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

随机推荐

  1. ios学习--TableView详细解释

    -.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [DataTa ...

  2. poj_2315 最小费用最大流

    题目大意 一个图上有N个顶点,从1到N标号,顶点之间存在一些无向边,边有长度,要求从顶点1走到顶点N,再从顶点N走回顶点1,其中不必要经过每个顶点,但是要求走的路径上的边只能经过一次.求出从1---& ...

  3. js实现表单项的全选、反选以及删除操作

    <html> <head> <title>test</title> <script language="javascript" ...

  4. Android ImageResizer:inSampleSize

    import android.annotation.TargetApi; import android.content.Context; import android.content.res.Reso ...

  5. WEB安全第六篇--千里之外奇袭客户端:XSS和HTML注入

    零.前言 最近做专心web安全有一段时间了,但是目测后面的活会有些复杂,涉及到更多的中间件.底层安全.漏洞研究与安全建设等越来越复杂的东东,所以在这里想写一个系列关于web安全基础以及一些讨巧的pay ...

  6. BluePrint和ORM

    一.蓝图创建 #引入库文件 from flask import Blueprint,request,jsonify user = Blueprint( "site", __name ...

  7. you-get 下载网络上的富媒体信息

    You-Get 乃一小小哒命令行程序,提供便利的方式,下载网络上的富媒体信息. 利用you-get下载这个网页的视频: $ you-get http://www.fsf.org/blogs/rms/2 ...

  8. 实用的IOS应用程序框架

    实用的IOS应用程序框架 目录 概述 概述

  9. Django - 模型层 - 下

    一.多表 sql 单表 多表 多对一 多对多 一对一 =============================================== 一对多:Book id title price p ...

  10. 解决 Ubuntu 下 Sublime Text 无法输入中文的问题

    解决 Ubuntu 下 Sublime Text 无法输入中文的问题 1. 安装依赖库 sudo apt-get install build-essential sudo apt-get instal ...