【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... ...
随机推荐
- JavaWeb基础: 会话技术简介
会话技术 用户使用Web应用的过程实际是调用了一系列的Servlet来组合处理请求,从而完成整个业务流.不同Servlet组合起来为用户服务的时候就会遇到一个数据共享和传输的问题,如何让多个Servl ...
- linux开关端口问题
linux开关端口问题: 我们知道一些常用的端口,比如mysql的端口为3306,sql的端口为:1433,以及tomcat的端口为 8008等等一样! 当这些端口在linux下是没有开启时,我们是无 ...
- target不起作用了
原因是 <a href="",target></a>中间多了个逗号.
- mvc伪静态<四> 伪静态后静态页面或者引用的css和图片失效
引用的css和图片失效的解决办法 把样式引用文件的相对路径改成绝对路径就可以了 比如原先的引用路径为:<link href="~/Content/css/style.css" ...
- case when 对某个字段值分类讨论
SELECT SM_ID,SM_CID,SM_STATION,SM_TIME,PS_CODE,PS_NUMBER,SS_NAME,SS_CODE, ( THEN '中转站' END) FROM dbo ...
- PHP函数——urlencode() 函数
urlencode($str)的作用是对字符串$str进行url编码,方便$str作为一个变量传递给下一页,一般情况下$str有两种, 第一种是数组类型,如果想将数组作为url的一个参数,即必须将数组 ...
- 项目管理办公室 PMO
项目管理办公室是组织中指导,协调,支持项目管理工作的一个常设职能部门,也就是管理项目管理的常设职能部门. 它负责指定和贯彻标准化的项目管理方法论(包括工作流程与规章制度等),协调所辖的各项目对资源,工 ...
- UVALive 7297 Hounded by Indecision BFS
题目链接:Hounded by Indecision 题意:map中给出小偷的位置,警察的位置.警察有一只狗,开始的时候警察和狗一起行动,也就是看做一个格子,当警察遇见小偷走过的格子时,狗就会嗅到它的 ...
- 使用Matrix控制图像或组件变换的步骤
1.获取Matrix对象,该Matrix对象既可新创建,也可直接获取其他对象内封装的Matrix(例如Transformation对象内部) 2.调用Matrix的方法进行平移.旋转.缩放.倾斜等. ...
- 在 Linux 中怎样将 MySQL 迁移到 MariaDB 上
自从甲骨文收购 MySQL 后,由于甲骨文对 MySQL 的开发和维护更多倾向于闭门的立场,很多 MySQL 的开发者和用户放弃了 MySQL.在社区驱动下,促使更多人移到 MySQL 的另一个叫 M ...