leetcode笔记:Best Time to Buy and Sell Stock III

一. 题目描述

Say you have an array for which the i-th element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

二. 题目分析

和前两道题相比,这道题限制了股票的交易次数,最多只能交易两次。

可使用动态规划来完成,首先是进行第一步扫描,先计算出序列[0, …, i]中的最大利润profit,用一个数组f1保存下来,这一步时间复杂度为O(n)

第二步是逆向扫描,计算子序列[i, …, n - 1]中的最大利润profit,同样用一个数组f2保存下来,这一步的时间复杂度也是O(n)

最后一步,对于,对f1 + f2,找出最大值即可。

三. 示例代码

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int maxProfit(vector<int> &prices)
    {
        int size = prices.size();
        if (size <= 1) return 0;

        vector<int> f1(size);
        vector<int> f2(size);

        int minV = prices[0];
        for (int i = 1; i < size; ++i)
        {
            minV = std::min(minV, prices[i]);
            f1[i] = std::max(f1[i - 1], prices[i] - minV);
        }

        int maxV = prices[size - 1];
        f2[size - 1] = 0;
        for (int i = size-2; i >= 0; --i)
        {
            maxV = std::max(maxV, prices[i]);
            f2[i] = std::max(f2[i + 1], maxV - prices[i]);
        }

        int sum = 0;
        for (int i = 0; i < size; ++i)
            sum = std::max(sum, f1[i] + f2[i]);

        return sum;
    }
};

四. 小结

相比前两题,该题难度稍大,与该题相关的题目有好几道。后续更新…

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 02:44:49

leetcode笔记:Best Time to Buy and Sell Stock III的相关文章

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

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 at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,

【leetcode】Best Time to Buy and Sell Stock III

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 algorithm to find the maximum profit. You may complete at most two transactions. Note:You may not engage in multiple

【LeetCode】Best Time to Buy and Sell Stock III (2 solutions)

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 algorithm to find the maximum profit. You may complete at most two transactions. Note:You may not engage in multiple

[LeetCOde][Java] 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 algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time

leetcode 【 Best Time to Buy and Sell Stock III 】python 实现

题目: 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 at most two transactions. Note:You may not engage in multiple transactions at the same time (

Leetcode 123. Best Time to Buy and Sell Stock III JAVA语言

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 at most two transactions. Note: You may not engage in multiple transactions at the same time (ie,

leetcode 123. Best Time to Buy and Sell Stock III ----- java

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 at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,

Java for 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 algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (ie,

LeetCode – Refresh – Best Time to Buy and Sell Stock iii

III is a kind of I. But it require 2 maximum value. So scan from begin and scan from end to record the maximum value for currrent index. Then scan the maximum array to obtain the global maximum. 1 class Solution { 2 public: 3 int maxProfit(vector<int

【leetcode刷题笔记】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 algorithm to find the maximum profit. You may complete at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,