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. 补第二周四人小组WBS/NABCD

    四人小组项目<东北师范大学论坛> 要求: 1.给出需求概述.功能列表.痛点或亮点.NABCD及WBS模型在此项目中的应用. 2.不熟悉的名词,自行搜索资料并参考教材第393页开始的术语索引 ...

  2. SSH基础(2)

    linux下配置 ssh运行的参数详解:

  3. Windows Store App 用户库文件操作

    (1)获取用户库位置 如果想要通过应用程序在用户库中创建文件,首先需要获得用户库中指定的位置,例如图片库.文档库等.这里值得注意的是,在获取用户库的位置之前,必须在Windows应用商店项目的清单文件 ...

  4. 如何提取HTML代码中img的src地址?

    答案:专门的代码 使用专门的正则表达式 /// <summary> /// 获得HTML中所有图片的src地址[比较稳定的一个版本] /// </summary> /// &l ...

  5. .net调用存储过程碰到的一个问题

    问题描述 报错信息如下: Execution of user code in the .NET Framework is disabled. Enable "clr enabled" ...

  6. apache本地网址配置

    1 实现类似于域名访www.a.com问本地的空间,而不是放在apache下的htocs文件夹下,或者是wamp下的www文件下 2 首先修改C盘WINDOWS\system32\drivers\et ...

  7. 枚举IoTimer

    /*************************************************************************************** * AUTHOR : ...

  8. Android统计图表MPAndroidChart.

    Android统计图表MPAndroidChart MPAndroidChart是在Android平台上开源的第三方统计图表库,可以绘制样式复杂.丰富的各种统计图表,如一般常见的折线图.饼状图.柱状图 ...

  9. Win7 Print Spooler服務自动关闭

    对于Win7系统而言,该问题通常是安装了错误的打印驱动引起的,Win7系统为了保护其它进程不受干扰,自动关闭了打印服务. 解决方法就是: a> 把不用的打印机删掉. b> 确保你安装了正确 ...

  10. 11个Linux基础面试问题

    Q.1: Linux 操作系统的核心是什么? Shell Kernel Command Script Terminal 答: 内核(Kernel)是Linux 操作系统的核心.Shell是一个命令行解 ...