【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 ...
随机推荐
- 细谈HTML5
回顾过了html接下来我们就来看看HTML5吧! HTML5手机应用的最大优势就是可以在网页上直接调试和修改.原先应用的开发人员可能需要花费非常大的力气才能达到HTML5的效果,不断地重复编码.调试和 ...
- PHP基本知识收集
1.符号“@”的作用 @是可以屏蔽函数执行过程中遇到问题而产生的一些错误.警告信息,这样用户就看不到程序的出错信息.这样除了用户界面会友好一些外,更重要的是安全性,因为屏蔽了出错文件的路径等信息. 2 ...
- JSP 客户端请求
当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信息头的一部分来传送的.您可以查阅HTTP协议来获得更多的信息. 下表列出了浏览器端信息头的一些重要内 ...
- EF中的那些批量操作
在使用EF的过程中,我们经常会遇到需要批量操作数据的场景,批量操作有的时候不仅能提高性能,比如使用SqlBulkCopy进入批量插入的时候,而且比较方便操作,提高效率.那么这篇文章就来总结EF中的那些 ...
- PHP IMAP收QQ邮件,SMTP存入另外QQ邮箱
作用,将qq1收到邮件,用qq2的账号.以qq0的为发件人身份放到qq2的邮箱. 什么样做这样一个功能,一个朋友要求的,她不告诉我为什么,好吧 <?php define('USER','xxx@ ...
- C# JavaScriptSerializer 解析Json数据(多方法解析Json 三)
准备工作: 1.添加引用System.Web.Extensions, 2..net3.5+版本都有,如果VS2010找不到,在这个文件夹找:C:\Program Files\Reference Ass ...
- JAVA异常体系
1.异常体系 ----|Throwable 所有错误或异常的父类 --------|Error(错误) --------|Exception(异常)一般能通过代码处理 ------------|运行时 ...
- zoj 2112 动态区间求第k大
题目大意: 动态单点更新,然后多次询问求区间内第k大 这里单个的主席树不能实现,这里采取的是树状数组套主席树 首先可以想的是将静态主席树先构建好,不去动它,这里空间复杂度就是O(nlogn),这个只要 ...
- 如何对Linux的grub进行加密
一.加密 设置grub密码: 众所周知,通过编辑GRUB启动参数可以轻松的进入单用户模式从而修改root密码,这对于一台多用户的计算机或服务器来说,无疑增加了安全隐患.大家一定很像为自己的GRUB加一 ...
- 《剑指offer-名企面试官精讲典型编程题》读后感
首先,不得不说这是一本好书!!! 我接触这本书是在学长的推荐下去看的,而且口碑还是挺好的一本书,豆瓣的评分也比较高,当我刚看了它,我就深深的爱上了这本书,到现在为止,我已经看了三遍这本书了,平时无聊时 ...