Description

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 (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

Input: [,,,,,]
Output:
Explanation: Buy on day (price = ) and sell on day (price = ), profit = - = .
Then buy on day (price = ) and sell on day (price = ), profit = - = .

Example 2:

Input: [,,,,]
Output:
Explanation: Buy on day (price = ) and sell on day (price = ), profit = - = .
Note that you cannot buy on day , buy on day and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.

Example 3:

Input: [,,,,]
Output:
Explanation: In this case, no transaction is done, i.e. max profit = .

问题描述: 给定一个数组,每一位代表当前索引的价格。设计一个算法,计算出最大利润。可以多次买卖。但是买卖不能在同一天操作

思路:这一题用到了贪心的思想。通过获取局部局部最优来达到整体最优解。找到递减区间的最低点,及最后一个点,同时再找到递增区间的最高点,也是最后一个点。把他们的差值累加,获得最大利润

public static int MaxProfit2(int[] prices)
{
int len = prices.Length;
int total = ;
int i = ;
while(i < len - )
{
int sell =, buy = ;
while (i + < len && prices[i + ] < prices[i])//获取递减区最低点(局部最小)
i++;
buy = i;//将最小点作为买入点
i++;//找下一个点,卖出点至少是买入点的下一个点
while (i < len && prices[i] > prices[i - ])//获取递增区最高点(局部最大)
i++;
sell = i - ;//获取卖出点
total += prices[sell] - prices[buy];
}
return total;
}

想法来源: https://www.cnblogs.com/grandyang/p/4280803.html

LeetCode Array Easy 122. Best Time to Buy and Sell Stock II的更多相关文章

  1. [LeetCode&Python] Problem 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 ...

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

  3. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

  5. 【刷题-LeetCode】122 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. 122. Best Time to Buy and Sell Stock II@python

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

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

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

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

随机推荐

  1. docker--container的port映射

    使用nginx为例 先运行nginx [root@localhost ~]# docker run --name web -d nginx Unable to find image 'nginx:la ...

  2. 【转】 linux下配置squid 服务器,最简单使用方式

    linux下配置squid 1.什么是squid Squid cache(简称为Squid)是一个流行的自由软件(GNU通用公共许可证)的代理服务器和Web缓存服务器.Squid有广泛的用途,从作为网 ...

  3. Linux下创建动态库与使用

    参考文章:dll和so文件区别与构成:http://www.cnblogs.com/likwo/archive/2012/05/09/2492225.html 动态库路径配置- /etc/ld.so. ...

  4. 代码解释n |= n >>> 16

    public static int nextPowerOf2(int n) {n -= 1;n |= n >>> 16;n |= n >>> 8;n |= n &g ...

  5. Java集合类框架的最佳实践有哪些

    根据应用的需要正确选择要使用的集合的类型对性能非常重要,比如:元素的大小是固定的,而且能事先知道,我们就应该用Array而不是ArrayList. 有些集合类允许指定初始容量.因此,如果我们能估计出存 ...

  6. 常用yum源之(Percona MySQL)

    [percona]name = CentOS $releasever - Perconabaseurl=http://repo.percona.com/centos/$releasever/os/$b ...

  7. Django框架的学习

    目前 Django 1.6.x 以上版本已经完全兼容 Python 3.x. 1. 指定django版本的安装 pip install django =1.11

  8. activemq消息中间件的依赖

    <dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artif ...

  9. 服务化改造的云上利器 | 阿里云 EDAS 重大升级发布

    11月22日,广东云栖大会企业级互联网架构专场上,阿里云发布了全新版本的企业级分布式应用服务EDAS. 新版本增强了对主流微服务框架的原生支持,实现SpringCloud & Dubbo用户代 ...

  10. Mybatis基于接口注解配置SQL映射器(二)

    Mybatis之增强型注解 MyBatis提供了简单的Java注解,使得我们可以不配置XML格式的Mapper文件,也能方便的编写简单的数据库操作代码.但是注解对动态SQL的支持一直差强人意,即使My ...