#-*- 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. 夺命雷公狗ThinkPHP项目之----企业网站7之栏目的修改(主要用模型来验证字段)

    我们照老,在控制器里面先查出我们所需要用到的数据: 然后直接遍历到模版上即可: 然后再开始写提交过来的数据处理问题(注意一定要接收修改页面通过隐藏域)而且我们刚才已经写好我们的model层了,所以直接 ...

  2. php file_get_contents与curl性能比较

    1.fopen /file_get_contents 每次请求都会重新做DNS查询,并不对 DNS信息进行缓存.但是CURL会自动对DNS信息进行缓存.对同一域名下的网页或者图片的请求只需要一次DNS ...

  3. C++头文件为什么要加#ifndef #define #endif

    #ifndef 在头文件中的作用 在一个大的软件工程里面,可能会有多个文件同时包含一个头文件,当这些文件编译链接成一个可执行文件时 ,就会出现大量“重定义”的错误.在头文件中实用#ifndef #de ...

  4. DOM4J解析xml案例

  5. OpenStack 物理资源问题

    Contents [hide] 1 写在前面 2 openstack的自有设置 3 解决办法 4 最终解决办法 写在前面 物理CPU核数为12,能虚拟多少虚拟核的机器?openstack的默认使用no ...

  6. Elasticsearch--Date math在索引中的使用

    在Elasticsearch,有时要通过索引日期来筛选某段时间的数据,这时就要用到ES提供的日期数学表达式 描述: 特别在日志数据中,只是查询一段时间内的日志数据,这时就可以使用日期数学表达式,这样可 ...

  7. android 设置Button或者ImageButton的背景透明 半透明 透明

    Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... />  透明 ...

  8. 在CentOS 6 32/64 上安装 PPTP 方式 VPN 服务

    网上有很多步骤, 讲了很多步骤,废话, 其实不如直接看代码, 而且也能直接运行,快速安装: rm -f /etc/pptpd.conf rm -f /etc/ppp arch=`uname -m` # ...

  9. 对EJB返回的AaaryList显示到table的处理方法

      1. ArrayList --> Object[]        ArrayList x = new ArrayList();        int i = x.size();        ...

  10. Environment中针对的读写权限判断

    Android应用开发中,常使用Environment类去获取外部存储目录,在访问外部存储之前一定要先判断外部存储是否已经是可使用(已挂载&可使用)状态,并且需要在AndroidManifes ...