题目描述

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

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

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

示例 1:

输入: [7,1,5,3,6,4]
输出: 7
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
  随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。

示例 2:

输入: [1,2,3,4,5]
输出: 4
解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
  注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
  因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。

示例 3:

输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。

题目分析

此题归类于贪心算法, 相对于best-time-to-buy-and-sell-stock,上一个题是用动态规划来做, 但是上一个题有一个限制就是不能在卖完股票的下一天买股票, 而此题没有这个限制。

这个题目的贪心策略就是 当买入一个卖出能赚钱, 就进行卖出操作

AC代码

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

为什么此时的贪心策略是最优策略?

请参考此博客

[leetcode122].为何此时贪心解是最优解 ?

LeetCode 122 best-time-to-buy-and-sell-stock-ii 详解的更多相关文章

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

  2. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

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

  3. LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)

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

  4. 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 an al ...

  5. leetcode 122. Best Time to Buy and Sell Stock II ----- java

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

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

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

  8. [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 an al ...

  9. Java for 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 an al ...

  10. LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)

    翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...

随机推荐

  1. Git、Github、Gitkraken 学习笔记

    <Git.Github.Gitkraken 学习笔记> 一.写在前面 1.参考资料 本文参考 <Pro Git> 一书. 在官网有免费在线版可供阅读:https://git-s ...

  2. vue : 本地调试跨域问题的解决办法:proxyTable

    本来我是不想写的,但为了加深印象还是写一写吧. ./config/index.js module.exports = { dev: { // Paths assetsSubDirectory: 'st ...

  3. 题解 洛谷 P3247 【[HNOI2016]最小公倍数】

    题意可以转化为是否能找一条从\(u\)到\(v\)的路径,经过的边的\(a\)和\(b\)的最大值恰好都是询问所给定的值. 若只有\(a\)的限制,可以将询问离线,对边和询问都从小到大排序,然后双指针 ...

  4. Netty 学习笔记(4) ------ EventLoopGroup

    EventLoopGroup负责管理Channel的事件处理任务,继承自java.util.concurrent包下的Executor,所以其结构类似与线程池,管理多个EventLoop. 而一个Ev ...

  5. JVM系列之:Contend注解和false-sharing

    目录 简介 false-sharing的由来 怎么解决? 使用JOL分析 Contended在JDK9中的问题 padded和unpadded性能对比 Contended在JDK中的使用 总结 简介 ...

  6. Java 并发实践 — ConcurrentHashMap 与 CAS

    转载 http://www.importnew.com/26035.html 最近在做接口限流时涉及到了一个有意思问题,牵扯出了关于concurrentHashMap的一些用法,以及CAS的一些概念. ...

  7. Linux切换用户时报错/.bash_profile: Permission denied,命令行(终端提示符)出现-bash-4.2$

    Linux切换用户时报错/.bash_profile: Permission denied,命令行(终端提示符)出现-bash-4.2$ 利用su - 切换用户时,发现有一个用户切时出现如下情况 [r ...

  8. linux root 与普通用户之间的切换

    test@ubuntu:~$ su Password:  root@ubuntu:/home/uu# 也可以是从root用户切换到普通用户.如果当前是root用户,那么切换成普通用户uu用以下命令:s ...

  9. (一)python 格式化 excel 格式

    需求: 客户通过 sftp 上传了一个 poc测试的 excel文件, 下到 云桌面 查看,发现一堆格式问题, 怎么办呢? 公司又不允许 吧文件下载到本地处理, 只能在 服务器上进行处理. 一堆的类型 ...

  10. Django学习路27_HTML转义

    谨慎使用 自动渲染语法 {{code|safe}} urls.py 中添加对应的函数 url(r'getcode',views.getcode) 在 views.py 中添加 def getcode( ...