【LeetCode OJ】Best Time to Buy and Sell Stock
Problem Link:
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/
We solve this problem using DP algorithm, which only scans the prices list once. So the algorithm is in O(n) where n is the length of prices list.
By given a list prices[0:n], we represent a trasaction as (b,s) where 0<=b<=s is the day to buy and b<=s<=n-1 is the day to sell.
However, we do not need a O(n) table to store all local optimal solution. Instead, We use only three variables to keep track of local optimal solution.
Suppose prices[0:i] have been scanned, we maitain following variables:
- l - The day with the lowest prices in prices[0:i]
- b - The buy day of the local optimal transaction for prices[0:i]
- s - The sell day of the local optimal transaction for prices[0:i]
We initialize l = b = s = 0, and for each i = 1...n-1, we update by following steps:
- If prices[i] < prices[l], then update the lowest price day l.
- We update b = l and s = i if one of the following conditions is satisfied.
- prices[i] > prices[s]
- prices[s] - prices[b] < prices[i] - prices[l]
The following code in python is accepted by oj.leetcode.com.
class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
"""
DP solution: suppose already have following informations in prices[0:i]:
1. Lowest price
2. Current profit presented by the buy price and the sell price
Then for prices[0:i+1], we can update as follows:
If prices[i] > prices[s]:
sell_price = prices[i]
buy_price = lowest_price
elif prices[i]-lowest_price > prices[s] - prices[b]:
buy_price = lowest_price
sell_price = prices[i]
elif prices[i] < lowest_price:
lowest_price = prices[i]
After scan all prices, we return sell_price - buy_price
The initial case:
lowest_price = prices[0]
buy_price = prices[0]
sell_price = prices[0]
And we scan prices from prices[1].
"""
# Special case: prices == []
if not prices:
return 0
# Initialize with prices[0]
b = s = l = 0
# Scan from i = 1 to n-1
for i in xrange(1, len(prices)):
if prices[i] < prices[l]:
l = i
elif prices[i] > prices[s] or prices[s] - prices[b] < prices[i] - prices[l]:
s = i
b = l
return prices[s] - prices[b]
【LeetCode OJ】Best Time to Buy and Sell Stock的更多相关文章
- 【LeetCode OJ】Best Time to Buy and Sell Stock III
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock II
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...
- LeetCode OJ 123. 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 OJ 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 OJ 121. 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 OJ:Best Time to Buy and Sell Stock II(股票买入卖出最佳实际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 OJ: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 IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
随机推荐
- Qt之QSystemTrayIcon
简述 QSystemTrayIcon类为应用程序在系统托盘中提供一个图标. 现代操作系统通常在桌面上提供一个特殊的区域,称为系统托盘或通知区域,长时间运行的应用程序可以显示图标和短消息. 简述 内容 ...
- Eclipse Maven Web Application 设置配置文件
默认的项目添加会有问题,各种版本和编译版本错误造成. 1.更改Maven编译版本 2.更改项目Facets针对的版本 3.更改Settings
- ARM流水线关键技术分析与代码优化
引 言 流水线技术通 过多个功能部件并行工作来缩短程序执行时间,提高处理器核的效率和吞吐率,从而成为微处理器设计中最为重要的技术之一.ARM7处理器核使用了典型三级流 水线的冯·诺伊曼结构,AR ...
- 3.1 关系数据库标准语言SQL综述
一.SQL语言的特点 SQL结构查询语言 1.综合统一: 2.高度非过程化:不需要指定存储路径 3.面向集合的操作方式 4.以同一种语法提供两种使用方式:独立语言.嵌入式语言 5.语言简单,易学易用 ...
- FZU 2216 The Longest Straight 模拟
题目链接:The Longest Straight 就是一个模拟就是这样,T_T然而当时恶心的敲了好久,敲完就WA了,竟然有这么简单的方法,真是感动哭了.......xintengziji...zhi ...
- [Js]碰撞运动
描述:撞到目标点弹回来(速度反转) 一.无重力的漂浮div var div1=document.getElementById("div1"); var iSpeedX=6; var ...
- python下的复杂网络编程包networkx的安装及使用
由于py3.x与工具包的兼容问题,这里采用py2.7 1.python下的复杂网络编程包networkx的使用: http://blog.sina.com.cn/s/blog_720448d30101 ...
- RM报表的打印偏移
自己摸索一下 RMReport1.SaveReportOptions.AutoLoadSaveSetting := True; RMReport1.SaveReportOptions.UseRegis ...
- Java 枚举&注解
枚举类 如何自定义枚举类 JDK1.5之前需要自定义枚举类 JDK 1.5 新增的 enum 关键字用于定义枚举类 若枚举只有一个成员, 则可以作为一种单例模式的实现方式 //枚举类 class Se ...
- 自动登录 登陆成功那个alert遮盖一直存在bug
手动登陆的时候,登陆成功MBProgressHUD message:@"登陆成功" 然后再dispatch_after 里调用MBProgressHUD hideHUD隐藏可以成功 ...