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. BUUCTF Youngter-drive

    exe逆向,首先查壳,发现有upx壳,upx -d脱壳,拖进ida找到主函数这里可以看到创建了两个线程,先沿着StartAddress,一直找到sub_411940,这里有一个问题,当使用f5是,会显 ...

  2. C#编程—第五天--循环语句for

    for穷举法.迭代法 穷举法练习: //穷举法: //1.找100以内的与7有关的数 //2.小明单位发了一百元的购物卡,他到超市买洗化用品,一是洗发水(15元),二是香皂(2元),三是牙刷(5元)怎 ...

  3. 关于UITableViewAutomaticDimension的产生的bug

      一.下面这句代码要想有作用 在iOS11之前需要适配,两个代理都需要实现 - (CGFloat)tableView:(UITableView *)tableView heightForHeader ...

  4. shell字符串的空值检查

  5. java 比较运算

    /* 比较运算符: 大于 > 小于 < 大于等于 >= 小于等于 <= 等于 == 不相等 != 注意事项: 1.比较运算符的结果一定是一个boolean值,成立就是true, ...

  6. Java集合体系结构(List、Set、Collection、Map的区别和联系)

    Java集合体系结构(List.Set.Collection.Map的区别和联系) 1.Collection 接口存储一组不唯一,无序的对象 2.List 接口存储一组不唯一,有序(插入顺序)的对象 ...

  7. JavaSE---环境配置

    1.概述 1.1 PATH环境变量 a,Java程序   编译.运行时   需要用到java.javac命令,虽然计算机中已经安装了JDK,但是计算机不知道去哪里找这个命令: b,计算机如何查找命令呢 ...

  8. 版本控制系统之SVN和GIT的区别

    版本控制器的作用: 1. 可以协同代码管理,让多人开发代码得以实现. 2. 回归到以前的任何一个时间点的代码处(好比:开始写了很多代码,后面有修改了一些,突然IDE崩溃,但是发现还是以前的代码更好,这 ...

  9. magento 站内优化和站外优化详解

    关于Magento如何如何我就不做评论了,一句话,谁用谁知道,搜索了下,百度和谷歌中文里还真没有一篇系统的关于magento seo的内容,花了一个上午的时间,稍微整理了一下,算是给新人一个指引吧.主 ...

  10. mysql学习-explain

    表头包含有: id---select_type---table---type---possible_keys---key---key_len---ref---rows---Extra id:selec ...