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 开发自定义一个提示框

    在开发的时候,会碰到很多需要提示的地方,提示的方法也有很多种,ios 8 以前的版本有alertview还是以后用的alertController,都是这种作用, 但是不够灵活,而且用的多了,用户体验 ...

  2. 【RF库Collections测试】Convert To List

    Name:Convert To ListSource:Collections <test library>Arguments:[ item ]Converts the given `ite ...

  3. CentOS 6.3下Samba服务器的安装与配置详解

    一.简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写,即为服务器消息块 ,SMB主要是作为Microsoft的 ...

  4. 关于font-size对垂直居中影响的问题

    背景:三个inline-block元素,其中两个内容为空,另外一个包含文字,设置文字的font-size之后,原本垂直居中的三个inline-block的元素,会变的不再垂直居中. 原因: 当设置了f ...

  5. Android N 7 【 classes.dex】反编译失败:com.googlecode.d2j.DexException: not support version.

    Microsoft Windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation.保留所有权利. D:\Android反编译工具[全]\2016\d ...

  6. Android内存优化总结【整理】

    http://blog.csdn.net/tiantangrenjian/article/details/39182293 [前段时间接到任务着手进行app的内存优化,从各种各样的渠道搜索相关资料,最 ...

  7. fluentValidation集成到autofac

    废话不说直接上代码 // 首先实现ValidatorFactory public class DependencyResolverValidatorFactory : ValidatorFactory ...

  8. IOS项目分层

    上传者:踏浪帅 分类:其他(Others) 查看次数:408 下载次数:70 上传时间:2016-01-07 大小:3 KB 主项目中的分层主要包含四个模块,Main(主要).Expand(扩展).R ...

  9. This function has none of DETERMINISTIC, NO SQL

    错误信息: [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declara ...

  10. (转)Python学习路径及练手项目合集

    转载自知乎 Wayne Shi,仅仅为了方便收藏查看,侵权删. 阶段1:入门知识 零编程基础的可以先从下面几个教程了解编程及环境入门知识.(已有编程基础直接从阶段2起步) 1. 编程新手指南2. Li ...