【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 problem using Greedy Algorithm, which only scan the prices list once. The worst-case running time is O(n).
According to the problem description, we know that the trasaction operations must be:
buy, sell, buy, sell, ..., buy, sell
For each day, we have two status, holding stocks or not, we have no stocks in day 0.
Then for each day i, we decide to buy or sell according to the prices of the following day:
- if we have no stock: we buy stock if prices[i] < prices[i+1]; otherwise, we do nothing.
- if we have stock: we sell stock if prices[i] > prices[i+1]; otherwise, we do nothing.
Note that we need to sell the stock in the last day if we have stock after scanning prices[0:n-1].
The python code is as follows.
class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
"""
We solve this problem using Greedy algorithm, since you can only buy one or sell one each day.
For each day, we have two possible status:
1 - I have stocks, I need to choose sell or not: if prices[i] > prices[i+1] then sell
2 - I do not have stocks, I need to choose buy or not: if prices[i] < prices[i+1] then buy
"""
# Initially, we do not have any stock
has_stock = False
total_profit = 0
buy_price = 0
for i in xrange(len(prices)-1):
if has_stock:
if prices[i] > prices[i+1]:
total_profit += prices[i] - buy_price
has_stock = False
else:
if prices[i] < prices[i+1]:
buy_price = prices[i]
has_stock = True
if has_stock:
total_profit += prices[-1] - buy_price
return total_profit
【LeetCode OJ】Best Time to Buy and Sell Stock II的更多相关文章
- 【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
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ We solve this problem ...
- 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: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】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:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- 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刷题笔记】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】Populating Next Right Pointers in Each Node II
Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...
随机推荐
- jquery 设置css样式
$("#61dh a").css('color', 多个样式属性 var divcss = { background: '#EEE', width: '478px', mar ...
- 笔记6:winfrom连接sql server 进行数据交换
今天的作业是用winfrom窗体做一个留言板,如图: 要求和数据库有查询和添加功能.下拉框里的值是直接获取数据库中的值 一.连接数据库,获取表中数据 //创建一个存数据的表 DataTable tab ...
- php提示Fatal error: Call to undefined function imagecreate()
在php中imagecreate函数是一个图形处理函数,主要用于新建一个基于调色板的图像了,然后在这个基础上我们可以创建一些图形数字字符之类的,但这个函数需要GD库支持,如果没有开启GD库使用时会 / ...
- urlencode在url中的作用
urlencode编码能解决特殊字符的传输问题. 使用urlencode主要用于正常识别输入的汉字.空格以及其他特殊字符. 列如: 一产品名称为A&T Plastic,在产品列表中就产生了这样 ...
- 管理工具MongoVUE使用
连接数据库 管理数据库 查询 1,查询所有 2,查询命令窗口
- asp.net 页面局部刷新
<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptMana ...
- 安装Adobe Dreamweaver CS6 免序列号 官方破解版
Adobe Dreamweaver CS6 免序列号 官方破解版 Adobe Dreamweaver CS6是世界顶级软件厂商Adobe推出的一套可视化的网页开发工具,Dreamweaver CS6最 ...
- ASP.net 验证码(C#) MVC
ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...
- PowerMock遇到的问题——4
当我们在测试一个方法的构造方法的时候,有的时候内部需要new一些对象,这是就需要用到PowerMock.exceptNew(),这个方法,但有时候传的参数有关键字this,比如SAPPublisher ...
- c# datagridview按条件搜索查询过滤
DataView的RowFilter 实现过滤 根据文本框文字对datagridview的数据进行模糊查询, 其实也就是一个过滤 string qymc = textBox1.Text.ToStrin ...