122. Best Time to Buy and Sell Stock II ——LeetCode
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: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
题目大意:与上一题类似,只不过没有次数限制。求最大获利。
思路:如果一直递增,那么最后一个元素减去第一个元素就是当前区间最大获利,没疑问吧。如果类似于1 3 2 4这种山峰似的,将每个递增子区间的获利加起来,就是最大获利。
Solution中有个图片看一眼就知道了。
public int maxProfit(int[] arr) {
if (arr == null ||arr.length == 0) {
return 0;
}
int len = arr.length;
int i = 1;
int start = 0;
int res = 0;
while(i<len) {
while(i<len && arr[i] > arr[i-1]) {
i++;
}
res += Math.max(arr[i-1] - arr[start], 0);
start = i;
i++;
}
return res;
}
122. Best Time to Buy and Sell Stock II ——LeetCode的更多相关文章
- 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 ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 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 ...
- 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 ...
- 【刷题-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 ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- [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 ...
- !!!!!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 ...
- 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 ...
随机推荐
- 【c#文档】在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
[c#文档]https://msdn.microsoft.com/zh-cn/library/system.convert.toint32.aspx 转载自:http://www.cnblogs.co ...
- 【python数据分析】利用Anaconda在window上搭建数据分析环境
由于在进行数据分析过程中,需要安装一些第三方库,导致python总会报一些错误,现将通过利用Anaconda搭建数据分析环境,已测可用. 1.到官网上下载python:https://www.pyth ...
- mysql存储过程嵌套循环并分页处理数据
业务背景:公司存证产品升级,随着数据量的增加,存证产品线按业务分表,导致以往的存证关联数据需要做数据同步更新.版本发布前,通过当前存储过程解决数据升级问题. ##创建存证文档关联情况下更新所用存储过程 ...
- JS的从理解对象到创建对象
JavaScript不是一门真正的面向对象语言,因为它连最基本的类的概念都没有,因此它的对象和基于类的语言中的对象也会有所不同.ECMA-262把对象定义为:“无序属性的集合,其属性可以包含基本值.对 ...
- ul+js模拟select
html css .select_box{ float: left; } .select_box input{ width: 160px; height: 30px; text-align: ce ...
- 基于RBAC模式的权限管理系统设计概要
很多人都知道以角色为基础的权限管理设计(RBAC),但是大部分人似懂非懂,不知道完整的权限管理系统都包括哪些内容. 在此以权限管理的使用场景来说明一下完整的权限管理内容. 一是鉴权管理,即权 ...
- scss-null在@mixin传参中的应用
可以给混合器声明参数,以便灵活的配置相关属性值,看如下scss代码: @mixin antzone-div($color, $font-size) { color:$color; font-size: ...
- 可编辑DIV 光标位置 处理
//场景: 要做一个网页即时通信,发送信息的文本编辑框 要求能发图片和表情,那么textarea就不能满足需求了,因为textarea内没有办法加入image // 采用方案是使用可编辑的DIV(也就 ...
- javascript12个你必须掌握的技能
网站建设的时候,作为码农,总喜欢写一些高效且省事的代码,这里,dbestech为你提供关于JavaScript的使用技巧点. 1. 空(null, undefined)验证 当我们创建了一个新的变量, ...
- show与ShowDialog substring
substring public String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串.该子字符串从指定的 beg ...