问题:

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. Java,来源于大神

    也许你学习了那么久的Java了,但如果有人问你什么是JavaEE?你会怎么回答他呢?在此我来谈谈关于JavaEE的相关技术.(仅是个人见解) 在谈JavaEE时,我们首先来了解一下Java平台.目前, ...

  2. Python学习笔记 之 函数

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

  3. BZOJ 3230: 相似子串

    3230: 相似子串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1485  Solved: 361[Submit][Status][Discuss ...

  4. Geolocation API JavaScript访问用户的当前位置信息

    Geolocation API在浏览器中的实现是navigator.geolocation对象,常用的有以下方法. 1.第一个方法是getCurrentPosition() 调用这个方法就会触发请求用 ...

  5. POJ 2942 Knights of the Round Table

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 10911   Acce ...

  6. Problems about trees

    Problems (1) 给一棵带边权的树,求遍历这棵树(每个节点至少经过一次)再回到起点的最短路程. 答案是显然的:边权之和的两倍. (2)给一棵带边权的树,求遍历这棵树(每个节点至少经过一次)的最 ...

  7. linux中给PHP安装mongodb的扩展

    centos5.6 32bit php 5.2.17 php安装路径 /usr/local/php phpize路径 /usr/bin php-config路径 /usr/bin php.ini路径 ...

  8. Dell服务器安装OpenManage(OMSA)

    公司上架了一批戴尔服务器,公司要求对这些服务器的硬件做一系列的监控,如CPU的温度,内存,风扇的状态,转速,磁盘等硬件的监控. 在对服务器的硬件监控上,目前业界主要基于如下两种: 1.服务器自带的工具 ...

  9. Solr学习总结(七)Solr搜索引擎的整体架构

    经过前面一段时间的努力,终于把我所知道的关于solr 的内容都总结完了.前面讲到了solr 的安装配置,web管理后台的使用,solr 的查询参数和查询语法,还说到了solr的客户端 solrnet  ...

  10. [Unity] Shader - CG语言 流程控制语句

    CG语言中: 不支持 switch 语句(可以写,但不能很好的执行.) 循环语句中, 循环次数不能大于 1024 ,否则会报错. If...ELSE 条件判断语句: if (true) { } els ...