【leetcode❤python】121. Best Time to Buy and Sell Stock
#-*- 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的更多相关文章
- 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...
- 【一天一道LeetCode】#121. Best Time to Buy and Sell Stock
# 一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say ...
- 【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 ...
- 【刷题-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 ...
- [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 ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- DMA-330(一)
DMA Controller的interface: DMA Controller提供这些feature: 1)instruction set,对DMA transfer进行program 2)AXI ...
- 嵌套错误Inline markup blocks (@<p>Content</p>) cannot be nested. Only one level of inline markup is allowed
例子: @{Html.Telerik().Splitter().Name("MainSplitter") .Orientation(SplitterOrientation.Vert ...
- Java如何对ArrayList里的元素排序
- SVN使用(二)
TortoiseSVN是windows平台下Subversion的免费开源客户端. 一般我们都是先讲讲服务器的配置,然后再讲客户端的使用,但是在TortoiseSVN上,却可以反过来.因为,如果你的要 ...
- XMl的解析
MainActivitypackage com.example.secondweek_test2; import java.io.BufferedInputStream; import java.io ...
- 【MFC三天一个游戏】之 局域网黑白棋
欢迎加入我们的QQ群,无论你是否工作,学生,只要有c / vc / c++ 编程经验,就来吧!158427611 花了三天上班时间,妈的上班写就是不能静下心来,擦,要防BOSS巡山.... 以前也写过 ...
- 转载WPF SDK研究 之 AppModel
Jianqiang's Mobile Dev Blog iOS.Android.WP CnBlogs Home New Post Contact Admin Rss Posts - 528 Artic ...
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- 遍历寻找json中的重复数据
string str = "[{\"ID\":1,\"Data\":{\"subjectCode\":\"1\" ...
- 使用sql创建表并添加注释
Create table T_ErrorLogTable_tb ( ELTID int identity(1,1) primary key,--编号 ELTTime date,--错误发生日期 ELT ...