Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).


题目标签:Array, Greedy

  这次的题目可以允许我们多次买卖,而 #121题 只允许我们买卖一次。

  回来更新一下解题思路,觉得之前的思路有一点繁琐,现在这个更简单明了。

  如何找到最大收益呢,我们可以把数字想象成对应高度的柱状图,当一整段array里,怎么才是最大收益呢?

  就是把所有上升区间里的 最大值(最后一个) 减去 最小值(第一个) 加在一起,就是最大收益值了。

  当我们遇到一个下降的数字,那么此时就是需要把之前的上升区间的profit 值加入maxProfit里的时候了。

  如果一直都是下降区间的话? 每次我们遇到一个下降数字,会把 前面一个数字end-1  减去 start 加入maxProfit,然后在更新start = end。

    所以遇到下降数字的时候,其实就是利用 一个数字减去自己,把0 加入maxProfit,不会影响答案。

  在遇到下降数字时候,start 和 end 不会分开;只有在上升区间里,start 和 end 才会分开,产生一个区间。

Java Solution:

Runtime beats 52.20%

完成日期:10/04/2017

关键词:Array, Greedy

关键点:找到所有的ascending range 把它们的profit 累加。

 class Solution
{
public int maxProfit(int[] prices)
{
if(prices == null || prices.length == 0)
return 0; int maxProfit = 0;
int start = 0;
int end = 1; while(end < prices.length) // iterate prices array
{
if(prices[end-1] > prices[end]) // if find any descending number
{
maxProfit += (prices[end-1] - prices[start]); // add the previous ascending profit
start = end; // update start pointer from this descending number
} end++;
} // take care the last part
maxProfit += (prices[end-1] - prices[start]); return maxProfit;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)的更多相关文章

  1. [LeetCode] 122. 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 ...

  2. 122 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II

    假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票) ...

  3. LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)

    翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...

  4. LeetCode 121. Best Time to Buy and Sell Stock (买卖股票的最好时机)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  5. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. 31. leetcode 122. Best Time to Buy and Sell Stock II

    122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...

  8. LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)

    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 122. Best Time to Buy and Sell Stock II ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. 百度编辑器不能插入html标签解决方法

    找到此方法: me.addInputRule(function (root) { var allowDivTransToP = this.options.allowDivTransToP; var v ...

  2. 【SQL】- 基础知识梳理(四) - 存储过程

    存储过程的概念 存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行 存储过程的好处 A. 存储过程允许标准组件式编程  ...

  3. 内核对象 windows操作系统

    问题: 什么是内核对象? 答:内核对象实际上时由内核分配的一块内存,而且只能由内核来访问.它时一个数据结构,成员包含有关于该对象的信息.一些成员对于所有对象类型都是一样的,比如对象名称.安全描述.使用 ...

  4. Linux Expect自动化交互脚本简介

    相关资料 维基百科:Expect SourceForge:The Expect Home Page TCL脚本言语简介 由于Expect是建立在TCL语言基础上的一个工具,因此首先检查一些TCL常见语 ...

  5. 3.bootstrap-组件

    1.图标 <button type="button" class="btn btn-default"> <span class="g ...

  6. windows下实现linux的远程访问以及linux上文件的上传和下载

    在网络性能.安全性.可管理性上,Linux有着其他系统无法比拟的强大优势,而服务器对这些方面要求特别高,因此Linux常常被用来做服务器使用.而当我们需要维护linux服务器的时候,就需要远程访问li ...

  7. 设计模式学习之“观察者模式” [C#]

    <深入浅出设计模式>学习笔记第二章 需求: 开发一套气象监测应用,如图: 气象站,目前有三种装置,温度.湿度和气压感应装置. WeatherData对象追踪气象站的数据,并更新到布告板,布 ...

  8. include 和require的区别

    相同点:include和require 都能把另外一个文件包含到当前文件中. 不同点:1.使用include时,当包含的文件不存在时,系统会报出警告级别的错误,程序会继续往下执行.   使用requi ...

  9. Android ListView getViewTypeCount 的返回值问题解决

    最近在学慕课网上的一个实战课程,期间有一个智能聊天机器人模块. 聊天界面通过 ListView 显示,用 Adapter 加载.一般来说,单对单的聊天,两者发出的话分别列在聊天页面的左右两边.所以,在 ...

  10. 演示 Calendar 的一般操作

    package com.yixin.webbrower; /* * 演示 Calendar 的一般操作 */ import java.util.Date; import java.text.Simpl ...