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:

  1. if we have no stock: we buy stock if prices[i] < prices[i+1]; otherwise, we do nothing.
  2. 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的更多相关文章

  1. 【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 ...

  2. 【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 ...

  3. 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 ...

  4. 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 ...

  5. 【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 ...

  6. 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 ...

  7. 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 ...

  8. 【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 ...

  9. 【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... ...

随机推荐

  1. phantomjs 乱码解决

    system = require('system') //传递一些需要的参数给js文件 address = system.args[1];//获得命令行第二个参数 ,也就是指定要加载的页面地址,接下来 ...

  2. servlet和web容器之间的关系

    Java是一种动态加载和运行的语言.也就是说当应用程序持有一个类的地址(CLASSPATH)和名称(包名和类名)的情况下,可以在程序运行期 间任何时候加载这个类,并创建和使用该类的对象.Servlet ...

  3. 盘点几种数据库的分页SQL的写法(转)

    Data序列——盘点几种数据库的分页SQL的写法http://www.cnblogs.com/fireasy/archive/2013/04/10/3013088.html

  4. 最小生成树练习3(普里姆算法Prim)

    风萧萧兮易水寒,壮士要去敲代码.本女子开学后再敲了.. poj1258 Agri-Net(最小生成树)水题. #include<cstdio> #include<cstring> ...

  5. SVG格式

    SVG格式 编辑 目 录 概述 简介 优势 实例 展现 1概述 SVG格式 SVG是一种用XML定义的语言,用来描述二维矢量及矢量/栅格图形.SVG提供了3种类型的图形对象:矢量图形(vectorgr ...

  6. C语言指针(一)

    一.指针 定义指针变量 指针指向的数据类型 *指针变量名称; 例: int *p; *作用: 1.在定义变量的时候 * 是一个类型说明符,说明定义的这个变量是一个指针变量 2.在不是定义变量的时候 * ...

  7. LSM树——放弃读能力换取写能力,将多次修改放在内存中形成有序树再统一写入磁盘

    LSM树(Log-Structured Merge Tree)存储引擎 代表数据库:nessDB.leveldb.hbase等 核心思想的核心就是放弃部分读能力,换取写入的最大化能力.LSM Tree ...

  8. [整理]Linux压缩与解压缩命令整理。

    一.压缩文件命令 1.*.Z compress 程序压缩的档案:2.*.bz2 bzip2 程序压缩的档案:3.*.gz gzip 程序压缩的档案:4.*.tar tar 程序打包的数据,并没有压缩过 ...

  9. J2EE面试题

    J2EE面试题 J2EE相关基础知识 1.面向对象的特征有哪些方面  1.  抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只 ...

  10. shell学习记录002-知识点储备

    1.echo "4*0.33" |bc    #计算机功能的运用 [root@oc3408554812 shell]# ss=22; [root@oc3408554812 shel ...