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. C++重载new和delete运算符

    内存管理运算符 new.new[].delete 和 delete[] 也可以进行重载,其重载形式既可以是类的成员函数,也可以是全局函数.一般情况下,内建的内存管理运算符就够用了,只有在需要自己管理内 ...

  2. PHP异步调用多线程

    $data["name"] = 'godlike';$data["age"] = 18; $post = http_build_query($data);$le ...

  3. 【转】PHP中获取当前系统时间、时间戳

    今天写下otime($time, $now)为将时间格式转为时间戳,$time为必填.清楚了这个,想了解更多,请继续往下看. 3. date($format)用法比如:echo date('Y-m-d ...

  4. 机器学习——AdaBoost元算法

    当做重要决定时,我们可能会考虑吸取多个专家而不只是一个人的意见.机器学习处理问题也是这样,这就是元算法(meta-algorithm)背后的思路. 元算法是对其他算法进行组合的一种方式,其中最流行的一 ...

  5. centos 7.0 安装nginx 1.117

    php官方下载地址 http://cn2.php.net/downloads.php

  6. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...

  7. Oracle基本查询语言

    --1.简单的数据查询语句--查询所有的员工的信息select * from emp;--查询员工的姓名和工作职位select ename,job from emp;--姓名和工作以中文的形式显示出来 ...

  8. window server 2008配置FTP服务器550 Access is denied. 问题解决办法

    如果你是按照网上的通用方法,在搭建FTP服务器的时候临时建了一个用户的话,那么这个用户是普通用户,你即便勾选了“读”“写”权限,那么再打开防火墙的情况下,你还是不能上传文件,只能下载.只需要登录到服务 ...

  9. WinForm下增加声音提示

    在WinForm平台下播放声音,一般有两种方式:第一种是调用系统自带声音: [代码] 申明定义: [DllImport("kernel32.dll")] public static ...

  10. IP分片详解

    IP分片是网络上传输IP报文的一种技术手段.IP协议在传输数据包时,将数据报文分为若干分片进行传输,并在目标系统中进行重组.不同的链路类型规定有不同最大长度的链路层数据帧,称为链路层MTU(最大传输单 ...