【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... ...
随机推荐
- Beautiful Soup教程 转
Python中使用Beautiful Soup库的超详细教程 转 http://www.jb51.net/article/65287.htm 作者:崔庆才 字体:[增加 减小] 类型:转载 时间:20 ...
- C类型
类型 32位 64位 char 1 1 short 2 2 int 4 4 long 4 8 指针 4 8 float 4 4 double 8 8 long 8 8 常用的基本 ...
- javaWeb学习之运用myeclipse结合tomcat开发一些简单的jsp和service
servlet是什么? servlet是java服务器端编程.不同于我们之前写的一般的java应用程序,Servlet程序是运行在服务器上的,服务器有很多种.....现在只是用过 tomcat ...
- MATLAB 随机生成互不重叠的多个矩形
建立m文件draw_rectangle.m. 其中p生成矩形的个数 function draw_rectangle(p) t = 1; x = rand(1)*10; y = rand(1)*10; ...
- BP神经网络
BP神经网络基本原理 BP神经网络是一种单向传播的多层前向网络,具有三层或多层以上的神经网络结构,其中包含输入层.隐含层和输出层的三层网络应用最为普遍. 网络中的上下层之间实现全连接,而每层神经元之 ...
- 【转】用PowerDesigner对现有的数据库反向工程建立E-R图
转自:http://www.cnblogs.com/oceanshare/archive/2010/02/10/1667071.html 由于早期的一个项目在数据库设计建立时没有输出为E-R图,偶在对 ...
- js 面试题
1.用原生js,创建一个无序列表添加到body中,ul下包含5个li,每个li包含一个text类型元素,text元素内容可自定义: <script type="text/javascr ...
- Android: Intent实现活动之间的交互
Intent的作用:是Android中各个组件直接交互的一种重要方式,且利用Intent可以启动Activity.Service以及Broadcast Receiver. Intent的创建:显示和隐 ...
- php 应用 cpu 100% 调试方法
找出进程占用cpu高的原因. 进程占用cpu高,一般是由于进程长时间占用cpu,又没有主动释放占用.如果想主动释放cpu,可以调用sleep.在写程序的时候,尤其要注意while 等循环的地方. 找出 ...
- 识别低效率的SQL语句
1.返回行与逻辑读的比率 CREATE TABLE t as select * from dba_objects; --CREATE INDEX idx ON t (object_id); ---例1 ...