【刷题-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 given stock on day i.
Design an algorithm to find the maximum profit. 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 at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
             Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
             Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
             engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
Solution
将所有可以赚钱的交易都做掉
Approach1 peak-valley
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int ans = 0;
        if(prices.size() == 0)return ans;
        int peak = prices[0], valley = prices[0];
        int n = prices.size();
        int i = 0;
        while(i < n-1){
            while(i < n - 1 && prices[i] > prices[i+1])i++;
            valley = prices[i];
            while(i < n - 1 && prices[i] <= prices[i+1])i++;
            peak = prices[i];
            ans += peak - valley;
        }
        return ans;
    }
};
Approach2
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int ans = 0;
        for(int i = 1; i < prices.size(); ++i){
            if(prices[i] > prices[i-1])ans += prices[i] - prices[i-1];
        }
        return ans;
    }
};
【刷题-LeetCode】122 Best Time to Buy and Sell Stock II的更多相关文章
- 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 ... 
- [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 ... 
- 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 ... 
- 【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 ... 
- 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 ... 
- 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 ... 
- 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 ... 
- 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 ... 
- 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 ... 
- [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 ... 
随机推荐
- CF1059A Cashier 题解
			Content 定义一天长度为 \(L\),每次休息的时间为 \(a\).一天会有 \(n\) 个客人到访,第 \(i\) 个客人会在 \(t_i\) 的时刻到访,会停留 \(l_i\) 的时间.只有 ... 
- React Color使用
			需求 - 要在react项目中实现颜色获取器功能 解决方案 - 使用react-color 依赖 - git地址:https://github.com/casesandberg/react-color ... 
- SpringBoot整合knife4j框架(可生成离线接口文档),并设置接口请求头token默认值
			功能和swagger类似 官网地址:https://doc.xiaominfo.com/knife4j/ 这个框架可以设置返回字段的描述 引入依赖 <dependency> <gro ... 
- Dapr项目应用探索
			背景介绍 前面文章对Dapr的基本信息进行了学习,接下来尝试将Dapr应用相关应用中. 接下来一步步实现应用dapr功能. 一.预期效果 如上图应用Dapr点包含: a) 报表服务绑定统一数据源服务: ... 
- github fmt库语法+范例(fmt version :7.0.1)
			!!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist fmt fmt 源码: https://github.com/fmtlib/fmt 本文翻译 ... 
- 【LeetCode】1170. Compare Strings by Frequency of the Smallest Character 解题报告(C++)
			作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 日期 题目地址:https://leetc ... 
- D. Puzzles(Codeforces Round #362 (Div. 2))
			D. Puzzles Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 ... 
- 据库自增ID用完
			Mysql里int类型是4个字节,如果有符号位的话就是[-2^31,2^31-1],无符号位的话最大值就是2^32-1,也就是4294967295. 自增ID达到上限用完了之后,分为两种情况: 如果设 ... 
- Java Web大作业——编程导航系统
			title: Java Web大作业--编程导航系统 categories: - - 计算机科学 - Java abbrlink: 40bc48a1 date: 2021-12-29 00:37:35 ... 
- Scale-Invariant Error
			目录 概 主要内容 代码 Eigen D., Puhrsch C. and Fergus R. Depth Map Prediction from a Single Image using a Mul ... 
