`

Best Time to Buy and Sell Stock I II III(C++实现)

 
阅读更多
// Best Time to Buy and Sell Stock I
 
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if(prices.size() < 2) return 0;
        int mj = 0, mjIdx = 0;
        int ret = 0;
        for(int i = 1; i < prices.size(); ++i) {
            if(prices[i] >= prices[i-1]) {
                mj = prices[i] - prices[mjIdx];
            } else {
                if(prices[i] < prices[mjIdx]) {
                    mjIdx = i; 
                }
            }
            ret = max(ret, mj);
        }
        return ret;
    }
};
 
 
// Best Time to Buy and Sell Stock II
 
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if(prices.size() < 2) return 0;
        int ms = 0;
        int ret = 0;
        for(int i = 1; i < prices.size(); ++i) {
            if(prices[i] >= prices[i-1]) {
                 ms += prices[i] - prices[i-1];
            } else {
                ret += ms;
                ms = 0;
            }
        }
        ret += ms;
        return ret;
    }
};
 
// Best Time to Buy and Sell Stock III
 
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if(prices.size() < 2) return 0;
        int n = prices.size();
        vector<int> l(n);
        vector<int> r(n);
        
        int minv = prices[0];
        l[0] = 0;
        for(int i = 1; i < n; ++i) {
            minv = min(minv, prices[i]);
            l[i] = max(l[i-1], prices[i] - minv);
        }
        
        int maxv = prices[n-1];
        r[n-1] = 0;
        for(int i = n-2; i >= 0; --i) {
            maxv = max(maxv, prices[i]);
            r[i] = max(r[i+1], maxv - prices[i]);
        }
        
        int ret = 0;
        for(int i = 0; i < n; ++i) {
            ret = max(ret, l[i] + r[i]);
        }
        
        return ret;
    }
};

 

 

 欢迎关注微信公众号——计算机视觉 

 

 

0
0
分享到:
评论

相关推荐

    leetcode题目:Best Time to Buy and Sell Stock II

    《最佳买卖股票时机II》是LeetCode中的一道经典编程题目,主要涉及动态规划和数组操作的知识点。在这个问题中,我们被赋予一个整数数组`prices`,表示某支股票每天的价格。任务是找到在允许进行多次交易的情况下,...

    java-leetcode题解之Best Time to Buy and Sell Stock III.java

    在深入探讨“java-leetcode题解之Best Time to Buy and Sell Stock III.java”这一文件内容之前,我们需要了解LeetCode平台及其题解的价值。LeetCode是一个专门面向技术面试的在线编程平台,它提供了一系列的编程...

    java-leetcode题解之Best Time to Buy and Sell Stock I.java

    Best Time to Buy and Sell Stock I是LeetCode网站上的一道著名的编程题目,它属于动态规划的范畴。这道题目通常被称为“买卖股票的最佳时机”,主要是考察程序员对动态规划思想的理解和应用。在这个问题中,给定一...

    java-leetcode题解之Best Time to Buy and Sell Stock IV.java

    Java实现“Best Time to Buy and Sell Stock IV”的过程主要涉及动态规划思想的应用,以及在编写代码时对时间效率和空间效率的把握。该算法题不仅能够检验程序员的编程技能,还能够加深其对算法理论知识的理解和应用...

    java-leetcode题解之Best Time to Buy and Sell Stock II.java

    - 题目理解:Best Time to Buy and Sell Stock II这道题目的目的是寻找一个最佳的股票买卖时机,使得获得的利润最大化。 - 问题分析:此题是一道典型的动态规划问题,也可以使用贪心算法解决。 - 解题思路:贪心算法...

    java-leetcode题解之Best Time to Buy and Sell Stock with Cooldown

    在解决股票交易问题时,程序员们经常会遇到“Best Time to Buy and Sell Stock with Cooldown”这类问题。该问题属于动态规划的经典案例,通常被列为中等难度。在这类问题中,给定一个整数数组表示某只股票的价格,...

    js-leetcode题解之123-best-time-to-buy-and-sell-stock-iii.js

    在JavaScript中,LeetCode题目"Best Time to Buy and Sell Stock III"是一个典型的数据结构和算法问题,通常出现在编程面试和算法训练中。该问题的目标是在给定的股票价格数组中找出最大的利润,但是与简单买卖一次...

    python-leetcode题解之123-Best-Time-to-Buy-and-Sell-Stock-III

    python python_leetcode题解之123_Best_Time_to_Buy_and_Sell_Stock_III

    python-leetcode题解之122-Best-Time-to-Buy-and-Sell-Stock-II

    在探讨python实现leetcode第122题"Best Time to Buy and Sell Stock II"的解决方案前,我们首先需要明确题目要求。第122题属于动态规划和贪心算法范畴,要求编写一个程序,通过分析给定的股票价格数组来找出最大的...

    javalruleetcode-leetcode-java:力码笔记

    java lru leetcode leetcode-java leetcode刷题笔记 ...III 141.Linked List Cycle 142.Linked List Cycle II 188.Best Time to Buy and Sell Stock IV 217.Contains Duplicate 263.Ugly Number 264.Ugly Number II

    lrucacheleetcode-LeetCode:CppSourceCode的LeetCode解决方案

    Best Time to Buy and Sell Stock 122 买卖股票的最佳时机 Ⅱ Best Time to Buy and Sell StockⅡ 123 买卖股票的最佳时机 Ⅲ Best Time to Buy and Sell StockⅢ 188 买卖股票的最佳时机Ⅳ Best Time to Buy and ...

    3、动态规划必练题(含解法).pdf

    - Best Time to Buy and Sell Stock III:存在多日限制交易,最多两次交易。 - Best Time to Buy and Sell Stock IV:与前三者不同,可能需要多次买入和卖出。 - Best Time to Buy and Sell Stock with Cooldown...

    js-leetcode题解之122-best-time-to-buy-and-sell-stock-ii.js

    本篇文档将深入分析LeetCode 122号题目,即“买卖股票的最佳时机 II”,并给出一种JS语言的解决方案。此题目要求我们计算在只允许进行一次交易的情况下(即买一次和卖一次),如何获取最大的利润。 该问题的核心是...

    股票收益leetcode-leetcode:leetcode摘要

    股票收益leetcode LeetCode 股票问题 Best Time to Buy and Sell Stock ...一次交易,找最大收益 ...i ...i) ...i-low) ...i-buy1) #找第一次交易最大收益 buy2 = min(buy2, i-prof1) #找用第一次收益购买的股票仍

    python-leetcode题解121-Best-Time-to-Buy-and-Sell-Stock

    为了实现这一目标,我们需要采用一种能够回顾所有历史价格并决定最佳买卖点的策略。 接下来,我们来构建一个解决该问题的框架。考虑到这是一个典型的最大子序列和问题,可以通过一次遍历来完成。在遍历过程中,我们...

    js-leetcode题解之121-best-time-to-buy-and-sell-stock.js

    实现的关键在于动态规划的思想,需要维护两个变量:一个记录到当前为止的最低股票价格,另一个记录到当前为止的最大利润。算法的实现步骤如下: 首先,初始化两个变量,最低价格minPrice设为无穷大,最大利润...

    Andy619-Zhu#CS-Notes-chu#63. 股票的最大利润1

    63. 股票的最大利润题目链接Leetcode:121. Best Time to Buy and Sell Stock题目描述可以有一次买入和一次卖出,买入必

    Clean C++: Sustainable Software Development Patterns and Best Practices

    It is written to help C++ developers of all skill levels and shows by example how to write understandable, flexible, maintainable, and efficient C++ code. Even if you are a seasoned C++ developer, ...

Global site tag (gtag.js) - Google Analytics