问题:

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

 

这道题是Best Time to Buy and Sell Stock I问题的第二个版本,题目整体的要求没有变,和I一样,只是在这里不再只限一次交易,此题中可以多次交易,只是买之前必须先卖出去,且初始手里没有股票。

 

分析:

在这道题中由于买卖的次数是不做限制的,而且是不买卖股票是不收手续费的,所以只要第二天的价格比第一天低我们就可以买,比如:

数组是[7,8,9,1,2,3,2,1]

在这个数组中我们可以7买入,9卖出,再1买入,3卖出,这就是我们能找到的最大收益了。

但是这和我提到的第二天比第一天低我们就买有什么关系呢?

7买9卖,我们可以这么看,

第一天,价格为7,第二天价格8>7,所以我们7买入,花了7;

第二天,价格为8,首先把第一天买的买了,然后再看9>8,然后又8买入,没赚;

第三天,价格为9,把第二天8买的买了,然后再看1<9,不买了,赚了2;

以此类推。。。

所以这道题说白了就是然你找到递增的子数列,然后所有递增子数列最大-最小的和就是最大收益。

 

代码如下(java):

public class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length == 0)return 0;
int profit = 0;
for(int i=1; i<prices.length;i++){
if(prices[i]>prices[i-1]){
profit += prices[i]-prices[i-1];
}
} return profit;
}
}

LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)的更多相关文章

  1. LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)

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

  2. LeetCode——Best Time to Buy and Sell Stock I (股票买卖时机问题1)

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

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

  4. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

  5. LeetCode: Best Time to Buy and Sell Stock II 解题报告

    Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...

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

  7. [leetcode]Best Time to Buy and Sell Stock II @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...

  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: Best Time to Buy and Sell Stock II [122]

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

随机推荐

  1. Python学习笔记 之 函数

    函数 函数式编程最重要的是增强代码的重用性和可读性  定义和使用 def 函数名(参数): ... 函数体 ... 返回值 函数的定义主要有如下要点: def:表示函数的关键字 函数名:函数的名称,日 ...

  2. 【BZOJ-2668】交换棋子 最小费用最大流

    2668: [cqoi2012]交换棋子 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1055  Solved: 388[Submit][Status ...

  3. 【bzoj4517】 Sdoi2016—排列计数

    http://www.lydsy.com/JudgeOnline/problem.php?id=4517 (题目链接) 题意 求n个数中正好m个数位置不变的排列数. Solution $${错排公式: ...

  4. POJ2417 Discrete Logging

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. ztree.js的使用整理

    /** 配置:知识点管理 */ var setting = { view: { showIcon: false, addDiyDom: addPrevDom, addHoverDom: addHove ...

  6. c#.Net:Excel导入/导出之NPOI 2.0简介

      NPOI 2.0+主要由SS, HPSF, DDF, HSSF, XWPF, XSSF, OpenXml4Net, OpenXmlFormats组成,具体列表如下: 资料来自:百度百科   Ass ...

  7. MySQL主从同步延迟

    早上接到open-falcon报警,一台mysql从库同步延迟2w多秒,mysql版本比较老,用的5.1.37. 连接从库查找原因: show processlist一下,查看哪些线程在跑. 看到Ti ...

  8. My97DatePicker

    http://www.my97.net/index.asp <input id="txtDate" class="Wdate" type="te ...

  9. foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because 'System.Web.UI.WebControls.Table' does not contain a public definition for 'GetEnumerator'

    错误:foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because ' ...

  10. win7使用iis并搭建 图片服务器

    1.打开控制面板 2.程序-卸载程序 3.点击左边的 打开或关闭windows功能 4.如下图所示,找到internet信息服务勾选.顺便把FTP服务器也全部勾选了,后面会用到 5.进入 控制面板 – ...