[Algorithm] 122. Best Time to Buy and Sell Stock II
Previous one: https://www.cnblogs.com/Answer1215/p/11974453.html
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).
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.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 buyingInput: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
var maxProfit = function(prices) {
let sum = ;
for (let i = ; i < prices.length; i++) {
sum += Math.max(, prices[i] - prices[i-]);
}
return sum
};
[Algorithm] 122. Best Time to Buy and Sell Stock II的更多相关文章
- 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 ...
随机推荐
- 干货|Dubbo社区开发者日经验分享
Hello,各位小伙伴大家好,我是小栈君,昨天也就是2019年10月26日,有幸在成都参加了由阿里举办的"Dubbo社区开发者日". 本次活动汇聚了各方面的大神欢聚一堂,主要是对现 ...
- 安装Windows10出现无法识别磁盘时的解决方案
由于前些日子对deepin系统比较感兴趣,一时兴起把备用机刷成了deepin,奈何还是过分依赖windows下的软件,又不得不再刷回Win10. 但由于Linux支持的磁盘格式与Windows不同,在 ...
- 概述UML——UML系列篇一
前言 作为Java应用开发者,日益感觉到对象建模的重要性.系统的复杂性,对于不能全局掌握的我和编程时没有对象模型指导时,编写实现代码时,感觉甚是困难.处于这些原因,这里想借助学习UML建模,在分析需求 ...
- 记一次Spring boot集成mybatis错误修复过程 Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
最近自己写了一份代码签入到github,然后拉下来运行报下面的错误 Error starting ApplicationContext. To display the conditions repor ...
- 如何在Linux中复制文档
在办公室里复印文档过去需要专门的员工与机器.如今,复制是电脑用户无需多加思考的任务.在电脑里复制数据是如此微不足道的事,以致于你还没有意识到复制就发生了,例如当拖动文档到外部硬盘的时候. 数字实体复制 ...
- 当前标识(IIS APPPOOL\DefaultAppPool)没有对“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files”的写访问权限
找到或增加这个目录,给他增加权限.
- 编写可维护的JavaScript-随笔(七)
将配置数据从代码中分离出来 代码中有些数据有修改的可能,如果放在函数中的话后期修改的时候会带来一些不必要的风险 需要将配置数据从代码中抽取出来,如果配置数据多的话可以放入一个对象中,然后修改抽取出来的 ...
- 30、filter数组去重
eg: let arr=[1,0,0,9,7,7,5,2] let data=arr.filter((item,index,self)=> self.indexOf(item)===index ...
- 《JavaScript高级程序设计》笔记:高级技巧
高级函数 安全的类型检测 在任何值上调用Object原生的toString()方法,都会返回一个[object NativeConstructorName]格式的字符串.每个类在内部都有一个[[Cla ...
- JavaScript 简单类型和复杂类型区别
一.基本类型 1.概述 值类型又叫做基本数据类型,简单数据类型.在存储时,变量中存储的是值本身,因此叫做值类型 2.基本类型在内存中的存储 基本数据类型存储在栈区中. 3.基本类型作为函数的参数 基本 ...