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 ...
随机推荐
- 解决perl: warning: Setting locale failed.
在Ubuntu Server 12.04上执行apt-get install命令时,报如下warning 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- Retrofit2与RxJava用法大全
Retrofit2是square公司出品的一个网络请求库,网上有很多相关的介绍.我很久以前都想去研究了,但一直都有各种事情耽搁,现在就让我们一起去捋一捋,这篇主要讲解Retrofit2与RxJava的 ...
- 安装windows phone 7
本机环境win7 32位旗舰版,本来是4G内存的 系统只能读出2.9G,vs2010中文旗舰版,想搭建windows phone环境学习wp手机开发.安装完了之后明显感觉机器慢了些. ①:安装Mic ...
- C语言的一小步—————— 一些小项目及解析
——-------- 仅以此献给东半球第二优秀的C语言老师,黑锤李某鸽,希望总有那么一天我们的知识可以像他的丰臀一样渊博! bug跟蚊子的相似之处: 1.不知道藏在哪里. 2.不知道有多少. 3.总是 ...
- leetcode——2
1. 题目 Add Two Numbers You are given two linked lists representing two non-negative numbers. The digi ...
- IOS Block动画
● + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)dur ...
- python剑指offer剪绳子
题目 给你一根长度为n的绳子,请把绳子剪成m段 (m和n都是整数,n>1并且m>1)每段绳子的长度记为k[0],k[1],…,k[m].请问k[0]k[1]…*k[m]可能的最大乘积是多少 ...
- 记一次加密算法MD5
通过MessageDigest可以获取到16个字节数组: MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] ...
- Linux学习记录(二)
1.远程连接工具的使用 实际开发中,Linux服务器都在其他的地方,我们要通过远程的方式去连接Linux并操作它,Linux远程的操作工具有很多,企业中常用的有Puttty.secureCRT.SSH ...
- c#树形结构
http://download.csdn.net/detail/rememberme001/6997235 http://bbs.csdn.net/topics/310094150 http://ww ...