[LeetCode] 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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
更上一题不同的是,这次是想交易多少次就交易多少次,不过每次只能进行一次交易,即交易不能重叠。
采用贪心法,如果有赚头就执行一次交易,这样就可以累计所有的价格增益。
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
var prev = prices[0];
var profit = 0; prices.forEach(function(now) {
if (now > prev){
profit += now - prev;
}
prev = now;
}); return profit;
};
[LeetCode] Best Time to Buy and Sell Stock II的更多相关文章
- LEETCODE —— 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 ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
- LeetCode: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- [LeetCode] 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——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- [leetcode]Best Time to Buy and Sell Stock II @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...
- LeetCode: Best Time to Buy and Sell Stock II [122]
[题目] Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- LeetCode——Best Time to Buy and Sell Stock II
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
随机推荐
- saltstack初探
salt-key -y -d linux-node1 #删除linux-node1节点的认证 salt -G 'cpuarch:x86_64' grains.item num_cpus >> ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 初试PHP单元测试TDD之安装PHPUnit
东风吹战鼓擂,一年一度的校招季开始了,最为一名即将踏入社会的搬砖工,自然也闲不下来了.各种总结.恶补.面经在所难免.当遇见敏捷开发时,有点蒙了,这是什么东东,绝对不能吃!既然是一种软件开发的方式,听上 ...
- mysql中distinct的用法
本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...
- js中typeof和instanceof
对于typeof和instanceof,我们经常用来检测数据的类型.typeof可以检测Number.Boolean.String.Undefined类型,对于其他类型的数据都返回为object:而i ...
- windows XP上实现python2.7.5和python3.4.3共存
windows XP上实现python2.7.5和python3.4.3共存过程记录: 1. 首先安装python2.7.5和python3.4.3,两个版本安装顺序不分前后; 2. 检查系统环境变量 ...
- python调用其他程序或脚本方法(转)
python运行(调用)其他程序或脚本 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地 ...
- [转载]tomcat的配置文件server.xml不支持中文注释的解决办法
原文链接:http://tjmljw.iteye.com/blog/1500370 启动tomcat失败,控制台一闪而过,打开catalina的log发现错误指向了conf/server.xml,报错 ...
- Gridview中运用CommandField 删除控件时注意点
我在gridview1 <编辑列>里面添加了一个<CommandField 删除>的控件,之后在gridview1的事件<RowDeleting>事件下 写了一段删 ...
- MQTT(三)-----连接与心跳
MQTT协议笔记之连接和心跳 - 推酷 http://www.tuicool.com/articles/AFvmee 互联网推送服务原理:长连接+心跳机制(MQTT协议) - clh604的专栏 - ...