#-*- coding: UTF-8 -*-
#Method1 :超时
#class Solution(object):
#    def maxProfit(self, prices):
#      
#        maxprofit=0
#        for i in xrange(1,len(prices)):
#            tmprofit=prices[i]-min(prices[0:i])
#            maxprofit=tmprofit if tmprofit>maxprofit else maxprofit
#        return maxprofit
#    
#sol=Solution()
#print sol.maxProfit([7, 6, 4, 3, 1])
#:Method2:动态规划
###遍历数组时记录当前价格以前的最小价格curMin,记录昨天能够获得的最大利润maxProfit
###对于今天,为了能获得此刻的最大利润,显然只能卖,或者不做任何操作
###如果不做任何操作,显然还是昨天maxProfit
###如果卖掉今天天的股票,显然prices[i]-curMin
class Solution(object):
    def maxProfit(self,prices):                                                       
        curMin=prices[0]
        maxProfit=0
        for i in xrange(1,len(prices)):
            maxProfit=max(maxProfit,prices[i]-curMin)
            curMin=min(prices[i],curMin)
        return maxProfit

sol=Solution()
print sol.maxProfit([7, 1, 5, 3, 6, 4])

【leetcode❤python】121. Best Time to Buy and Sell Stock的更多相关文章

  1. 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...

  2. 【一天一道LeetCode】#121. Best Time to Buy and Sell Stock

    # 一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say ...

  3. 【leetcode】121. Best Time to Buy and Sell Stock(股票问题)

    You are given an array prices where prices[i] is the price of a given stock on the ith day. You want ...

  4. 【刷题-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 ...

  5. [LeetCode&Python] Problem 122. 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 ...

  6. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

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

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

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

随机推荐

  1. 手机端js实现滑块推动

    代码编写:(写的格式有点差,凑合看吧,但是功能是实现了的) <html><head><meta http-equiv="Content-Type" c ...

  2. [JAVA]在linux中设置JDK环境,ZendStudio,Eclipse

    1.准备JDK安装包 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载对应平台的tar.gz格式压 ...

  3. Attribute 与 Property 的区别

    网上的说法是: Property 是面向对象的概念,是Object的一部分. Attribute 是<input type="text"> type就是Attribut ...

  4. android 学习随笔十四(页面跳转与数据传递)

    1.activity 创建第二个Activity 需要在清单文件中为其配置一个activity标签 标签中如果带有这个子节点,则会在系统中多创建一个快捷图标 <intent-filter> ...

  5. 【python cookbook】【数据结构与算法】6.在字典中将键映射到多个值上

    问题:一个能将键(key)映射到多个值的字典(即所谓的一键多值字典[multidict]) 解决方案:如果想让键映射到多值,需要将这多个值保持到另一个容器如列表或集合中: >>> d ...

  6. Install DBD::mysql for Perl in XAMPP in Mac , solving errors

    我不知道 why,在 Mac 安装 DBI::mysql 总会报错 我为了给 cgi-bin 添加 mysql-perl 数据库支持,也是够麻烦的 make sure that mysql and m ...

  7. Oracle 单行函数

    一.什么是函数 任何东西,只要它能接收输入,对输入进行加工并产生输出,它就可以被称为函数. 二.单行函数简介 单行函数只对表中的一行数据进行操作,并且对每一行数据只产生一个输出结果.单行函数可以接受一 ...

  8. Java 使用 Redis | 菜鸟教程

    入门教程: http://www.runoob.com/redis/redis-java.html 中文手册: http://redis.readthedocs.io/en/2.4/index.htm ...

  9. 使用Xcode和Instruments调试解决iOS内存泄露

    转载自:http://www.uml.org.cn/mobiledev/201212123.asp  (或者http://www.cocoachina.com/bbs/read.php?tid=129 ...

  10. html的textarea控制字数小案例

    <h3>设计理念说明(200字以内)</h3> <textarea onkeyup="checkLen(this)"></textarea ...