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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. [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 ...

  7. 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 ...

  8. 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. ...

  9. [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 ...

随机推荐

  1. 个人对B/S项目的一些理解(三)--Servlet与Strust

    以下是我自工作以来,结合对C/S项目的认知,对B/S项目的一些理解. 如有不足或者错误,请各位指正.   由于个人一开始入门时是ASP.NET MVC,是一个比较完善.完整的框架,下面仅对JAVA的w ...

  2. 引用js或css后加?v= 版本号的用法

    <span style="font-size:14px;">css和js带参数(形如.css?v=与.js?v= 或 .css?version=与.js?version ...

  3. Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引

    因为内容比较多,所以每篇讲解一些内容,最后会放出全部代码,可以参考.操作中总会遇到各式各样的问题,个人对部分问题的研究在最后一篇 问题研究 里.欢迎大家探讨学习. 代码都经过个人测试,但仍可能有各种未 ...

  4. Zabbix自定义监控8080端口的连接数

    Zabbix自定义监控8080端口的连接数 一 zabbix自定义监控实现思路 实际上我们要想使用zabbix来监控一些服务的原理很简单,步骤分别是:1.写一个脚本用于获取待监控服务的一些状态信息2. ...

  5. 纯JS实现俄罗斯方块,打造属于你的游戏帝国

    纯JS俄罗斯方块,打造属于你的游戏帝国. 本文原始作者博客 http://www.cnblogs.com/toutou 俄罗斯方块(Tetris, 俄文:Тетрис)是一款电视游戏机和掌上游戏机游戏 ...

  6. Apache Torque入门学习

    Introduction Apache Torque is an object-relational mapper for java. In other words, Torque lets you ...

  7. JavaScript - 正则表达式

    正则表达式的大致匹配过程是:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功:一旦有匹配不成功的字符则匹配失败. 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默 ...

  8. Python 自动化入门 day1复习

    一.Python介绍 Python是1989年圣诞节期间龟叔创造的一种解释型语言. 最新的TIOBE排行榜 目前Python主要应用领域: 云计算: 云计算最火的语言, 典型应用OpenStack W ...

  9. C#操作mysql数据库

    转  http://www.jb51.net/article/43486.htm using System;using System.Configuration;using MySql.Data.My ...

  10. Virtualbox 上调整 Mac OS 分辨率 最简单方法

    Mac OS 分辨率:VBoxManage setextradata "Mac OS X 10.10" VBoxInternal2/EfiGopMode 3       ----代 ...