#-*- 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. sql 登录注入

    DataTable dt= SqlHelper.ExecuteDataTable(System.Data.CommandType.Text, String.Format("select * ...

  2. backend flow

    在PD之后,netlist中会多出很多DCAP元件(去耦电容,减少IR-Drop)或者filter cell(保证芯片均匀度要求) 还有一些antenna cell也就是一些diode用来泻流,防止天 ...

  3. 关于linux的systemd的一些事

    1. 输出运行失败的单元: systemctl --failed 2. 所有的单元文件存放在 /usr/lib/systemd/system/ 和 /etc/systemd/system/ 这两个目录 ...

  4. Linux设备驱动工程师之路——内核链表的使用【转】

    本文转载自:http://blog.csdn.net/forever_key/article/details/6798685 Linux设备驱动工程师之路——内核链表的使用 K-Style 转载请注明 ...

  5. spring Aop的一个demo

    面向切面是什么我就不说了. 上代码: package com.foreveross.service.weixin.test; import java.lang.annotation.Documente ...

  6. DateTimeUtil 工具类,android 和 java 通用

    import java.sql.Date;import java.text.SimpleDateFormat; public class DateTimeUtil { public final cla ...

  7. JS和CSS的多浏览器兼容(3)

    3.Javascript的浏览器兼容性问题 3.1 集合类对象问题说明:IE下,可以使用()或[]获取集合类对象; Safari及Chrome下,只能使用[]获取集合类对象. 解决方法:统一使用[]获 ...

  8. 转载WPF SDK研究 之 AppModel

    Jianqiang's Mobile Dev Blog iOS.Android.WP CnBlogs Home New Post Contact Admin Rss Posts - 528 Artic ...

  9. Positional parameter are considered deprecated; use named parameters or JPA-style positional parameters instead.

    这行代码: List<Cat> catList =session.createQuery("from Cat p where p.name.first_name=?") ...

  10. JavaEE基础(二十二)/IO流

    1.IO流(序列流) 1.什么是序列流 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推. 2.使用方式 整合两个: ...