1. 题目

1.1 英文题目

You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. 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 simultaneously (i.e., you must sell the stock before you buy again).

1.2 中文题目

给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

1.3输入输出

输入 输出
prices = [7,1,5,3,6,4] 7
prices = [1,2,3,4,5] 4
prices = [7,6,4,3,1] 0

1.4 约束条件

  • 1 <= prices.length <= 3 * 104
  • 0 <= prices[i] <= 104

2. 实验平台

IDE:VS2019

IDE版本:16.10.1

语言:c++11

3. 分析

这一题可以借鉴121题的方法,也就是Kadane's Algorithm,具体来说就是求出相邻两个元素间的差值,组成数组,再将数组中的正值加到一起,即可。具体代码如下:

class Solution {
public:
int maxProfit(vector<int>& prices) {
int maxpro = 0;
for (int i = 1; i < prices.size(); i++)
maxpro += max(0, prices[i] - prices[i - 1]);
return maxpro;
}
};

Leetcode No.122 Best Time to Buy and Sell Stock II Easy(c++实现)的更多相关文章

  1. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

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

  3. 【刷题-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 ...

  4. 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

  6. 【LeetCode】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 a ...

  7. 【leetcode】122.Best Time to Buy and Sell Stock II(股票问题)

    You are given an integer array prices where prices[i] is the price of a given stock on the ith day. ...

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

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

随机推荐

  1. 基于Jira的运维发布平台的设计与实现

    作者:乔克 公众号:运维开发故事 上线发布是运维的日常工作,常见的发布方式有: 手动发布 Jenkins发布平台 Gitlab CI ...... 除此之外还有需要开源软件,他们都有非常不错的发布管理 ...

  2. unity中UI坐标转3d世界坐标

    方法: public static Vector3 UIScreenToWorldPoint(Vector3 uiPostion) { uiPostion = UICamera.mainCamera. ...

  3. IOS 小组件(7):小组件点击交互

    引言   前面我们似乎掌握了实现一个小组件所需要的一切技能,默认情况下桌面点击小组件,也正常跳转到了App中.接下来我们一起来看看,小组件是怎么做到点击跳转到App的. 点击交互方式 点击Widget ...

  4. pika详解(二) BlockingConnection

    pika详解(二) BlockingConnection   本文链接:https://blog.csdn.net/comprel/article/details/94592348 版权 Blocki ...

  5. C# 移除字符串头尾指定字符

    1 private void button1_Click(object sender, EventArgs e) 2 {//去掉字符串头尾指定字符 3 string MyInfo= "--中 ...

  6. 在Visual Studio 中使用git——分支管理-下(九)

    在Visual Studio 中使用git--什么是Git(一) 在Visual Studio 中使用git--给Visual Studio安装 git插件(二) 在Visual Studio 中使用 ...

  7. ONNX MLIR方法

    ONNX MLIR方法 MLIR中的开放式神经网络交换实现. Prerequisites gcc >= 6.4 libprotoc >= 3.11.0 cmake >= 3.15.4 ...

  8. Darknet_Yolov3模型搭建

    Darknet_Yolov3模型搭建 YOLO(You only look once)是目前流行的目标检测模型之一,目前最新已经发展到V3版本了,在业界的应用也很广泛.YOLO的特点就是"快 ...

  9. CodeGen准备存储库

    CodeGen准备存储库 CodeGen几乎总是与提供用于生成源文件的元数据的存储库结构一起使用,并且许多令牌需要使用存储库结构. 基本要求是有一个结构定义,并且该结构定义包含一个或多个字段定义.有些 ...

  10. 构造无限级树的框架套路,附上python/golang/php/js实现

    目录 前言 需求 数据 结果 框架 递归框架 迭代框架 递归框架实现 python golang php js 迭代框架实现 python golang php js 前言 框架思维非常重要,和语言无 ...