leetcode 【 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 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.
代码:oj测试代码 Runtime: 111 ms
class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
# none case or one element case
if prices is None or len(prices)<2 :
return 0
# dp
min_buy = 0
max_profit = 0
for i in range(1,len(prices)):
if prices[i]<prices[min_buy]:
min_buy = i
max_profit = max(prices[i]-prices[min_buy],max_profit)
return max_profit
思路:
典型的动态规划题目。
跟求最小子集的和这道题(http://www.cnblogs.com/xbf9xbf/p/4240510.html)类似。
个人认为这类题目的精髓在于“在每一步,我们维护两个变量,一个是全局最优,就是到当前元素为止最优的解是,一个是局部最优,就是必须包含当前元素的最优的解”。
对于这道题来说,有一个小梗需要想明白:如果prices[i]就是到i为止最小的值,那么prices[i]-prices[min_buy]不是等于0了么?
请注意,回顾动态规划的规则:一定要包含当前元素的最优解。既然当前元素已经是历史最小值了,那么还非要包含当前元素的最优解,不正是他自身减去自身么?
维护一个max_profit(最大利润),一个min_buy(历史最低点)。
1. 如果当前的prices[i]小于prices[min_buy],则更新min_buy。
2. 取prices[i]-prices[min_buy]和max_profit中较大的一个,作为max_profit的值。
最后返回max_profit即可。
leetcode 【 Best Time to Buy and Sell Stock 】python 实现的更多相关文章
- [leetcode]Best Time to Buy and Sell Stock @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ 题意: Say you have an array for ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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] 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 ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- LeetCode Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
随机推荐
- Eucalyptus常用查询命令
前言: Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...
- JS修改地址栏参数实例代码
function changeURLPar(destiny, par, par_value) { var pattern = par+'=([^&]*)'; var replaceText = ...
- react爬坑之路(一)--报错output.path不是绝对路径
之前,一直在纠结是学习angular好,学习vue好,还是学习react好,网上一搜索,也是各种对比,各种互喷,看过之后更纠结.就跟小时候一样纠结长大了是上清华好,还是上北大好,最后证明我想多了.总之 ...
- pat甲级1114
1114 Family Property(25 分) This time, you are supposed to help us collect the data for family-owned ...
- tomcat 启动显示指定的服务未安装
解决方法: 命令行进入tomcat的bin目录 输入“service.bat install” 重启,OK
- Android(java)学习笔记89:Bundle和Intent类使用和交互
1. Bundle 和 Intent: Bundle只是一个信息的载体 将内部的内容以键值对组织 ,Intent负责Activity之间的交互自己是带有一个Bundle的.Intent.putE ...
- SSM框架快速搭建
1. 新建Maven项目 ssm 2. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...
- 解决mysql8小时无连接自动断掉机制
windows下打开my.ini,增加: interactive_timeout=28800000 wait_timeout=28800000 MySQL是一个小型关系型数据库管理系统,由于MySQL ...
- red hat的防火墙怎么关闭
查看是否开启: service iptables status 关闭方法: service iptables stop 永远关闭: Ntsysv 把iptables前的*号去掉. 查看SELinux状 ...
- java 字符串中是否有数字
http://www.cnblogs.com/zhangj95/p/4198822.html http://www.cnblogs.com/sunzn/archive/2013/07/12/31865 ...