题目

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

Subscribe to see which companies asked this question

分析

买卖股票II,与LeetCode(121) Best Time to Buy and Sell Stock的区别就是,此次允许买卖多次,但是必须卖出后才可下次买入。

实际上是求股价波浪线的所有上升段的长度和。

AC代码

class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty())
return 0; int sum = 0;
//只要后项比前项大,这部分值就一定可以变为利润
for (size_t i = 1; i < prices.size(); ++i)
{
if (prices[i] > prices[i - 1])
sum += (prices[i] - prices[i - 1]);
}
return sum;
}
};

GitHub测试程序源码

LeetCode(122) Best Time to Buy and Sell Stock II的更多相关文章

  1. LeetCode(123) Best Time to Buy and Sell Stock III

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  2. LeetCode(121) Best Time to Buy and Sell Stock

    题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...

  3. leetcode 121 122 123 . Best Time to Buy and Sell Stock

    121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...

  4. Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)

    Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...

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

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

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

  8. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  9. 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

随机推荐

  1. 关于使用rancher部署k8s集群的一些小问题的解决

    问题一: 在rancher的ui上,不能创建k8s的master节点的高可用集群.创建k8s集群,添加节点的时候,可以添加多个master,但是多个master又没有高可用,只要其中一个出问题了,那么 ...

  2. 038 Count and Say 数数并说

    数数并说序列是一个整数序列,第二项起每一项的值为对前一项的计数,其前五项如下:1.     12.     113.     214.     12115.     1112211 被读作 " ...

  3. Eclipse启动SDK Manager报错:[SDK Manager] 'xcopy' 不是内部或外部命令,也不是可运行的程序。

    解决方法,在path环境变量下加上C:\WINDOWS\system32;或将C:\WINDOWS\system32\xcopy.exe拷贝到android sdk目录的tools下面即可正常运行.

  4. windows 用VMware创建linux虚拟机,安装操作系统CentOS7.2

    1.按照向导创建虚拟机 以下是安装虚拟机的步骤,没有写的直接下一步 [1]主页-创建新虚拟机 [2]选择 自定义(高级) [3]选择稍后安装操作系统 [4]给虚拟机命名并指定所在位置 [5]给处理器配 ...

  5. windows 安装 jdk1.8并配置环境变量

    1.查看电脑环境 我的电脑--右键--属性 2.下载jdk1.8 网址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-do ...

  6. 原创 html动态表格

    <table id="opttb"> <asp:Repeater ID="tempOptions" runat="server&qu ...

  7. 树莓派2安装Xware实现迅雷远程下载

    首先,远程功能很实用,尤其是基于迅雷的,现在国内的下载基本上迅雷只手遮天,别的工具友好程度不理想,这是对于我这种小白来说. 首先,我的树莓派系统不是原生的,我烧写的是ubuntu16,没有桌面,没有多 ...

  8. AngularJS(四):控制器、事件

    本文也同步发表在我的公众号“我的天空” 控制器 控制器可以说是AngularJS中最重要的部分了!之前的一些示例,除了第一讲的示例以外,我们对于AngularJS的使用都集中在HTML部分,其实Ang ...

  9. 登录控制 BaseController

    执行方法前 判断 sessin 登录信息 是否为空 ,空的话 返回 登录界面 并且给 LoginUser 赋值 public abstract class BaseController : Contr ...

  10. jsp另外五大内置对象之config

    //配置web.xml <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=&q ...