[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 ...
随机推荐
- Linux VIM8.1 Python3 编辑器配置文件
Linux VIM8.1 Python3 编辑器配置文件 实现功能: 自动补全(包括函数模块方法补全) 自动代码标准格式化 自动检查代码错误 自定义头文件 自动括号补全 缩进指示线 代码一键折叠 代码 ...
- DBA职业发展之路:去“IOE”等挑战之下,DBA将何去何从?
DBA职业发展之路:去“IOE”等挑战之下,DBA将何去何从? 摘要:随着近些年来,开源.自动化.云化的兴起,DBA职业也正悄然发生一些变化.面对一系列新的挑战,DBA将如何进行职业发展?本文将给出一 ...
- 汉诺(hanio)塔问题
规则:大盘子不能压在小盘子上.要求:将A柱子上所有盘(每个盘大小不同)放到C柱子上,使用B柱子作辅助. 比如柱子A上有n个盘,执行以下步骤: 1. 把n-1个盘从源柱移动到临时柱上: 2. 把源柱上剩 ...
- SQL Server 新增自动执行任务
第一步右击SQL Server代理,新建作业 第二步选择常规,给你要执行的计划命名 第三步选择步骤,然后给步骤命名,选择类型,数据库,输入你要执行的语句. 第四步设置要执行的频率,根据业务需要,一般建 ...
- C#安装和卸载windowsService的bat指令
只需新建2个文本文档,将2个指令分别复制进去,再将txt格式改为bat格式,以管理员身份运行 安装指令 %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\ ...
- http的GET方法参数中不能传列表,接收端的key会变
如下 async initTable() { await getHostAttributesForUser({'username': this.username}).then(response =&g ...
- English--动词语态
English|动词语态 动词的语态在理解长难句中很重要.了解被动语态与主动语态,掌握语态的变化方式. 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点的描述.力求不含任何的自我 ...
- box-shadow 模糊半径与扩展半径
关于box-shadow的基本用法参阅CSS3 box-shadow一章节. 此属性用来设置元素的阴影效果,语法结构如下: box-shadow:h-shadow v-shadow blur spre ...
- Cheat Engine 作弊表框架代码
打开游戏 打开自动汇编 扫描的所有过程,这里就省略了 引用作弊表框架代码 查找使阳光减少的地址 拷贝这个地址 添加到自动汇编脚本里,并添加汇编指令 分配到当前作弊表 生成自动汇编脚本 进行激活测试 可 ...
- Shell中根据svn是否有待更新的版本去决定是执行maven打包
1- svn 更新判断代码 本着学习的目的,这里使用了两种获取version的方法. localVersion=$(svn info -R | grep "Revision\:" ...