80.股票市场交易策略优化<字节青训营-中等题>

89 阅读5分钟

1.题目

问题描述

为了奖励你在上个季度的优秀表现,公司决定发给你一部分股票,你可以在市场上自由交易。

给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格。

设计一个算法计算出最大利润。

在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):

  • 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)
  • 卖出股票后,你无法在第二天买入股票(即冷冻期为 1 天)

输入格式

一个数组:[1,2,3,0,2]

数据约束

见题目描述

输出格式

最大的收益数值

输入样例

[1,2]

[2,1]

[1,2,3,0,2]

[2,3,4,5,6,7]

[1,6,2,7,13,2,8]

输出样例

1

0

3

5

12

2.思路

动态规划

状态定义

我们可以定义三个状态来表示我们在某一天的利润情况:

  1. hold[i] : 表示第 i 天持有股票时的最大利润。
  2. sell[i] : 表示第 i 天卖出股票时的最大利润。
  3. cooldown[i] : 表示第 i 天处于冷冻期(即前一天卖出之后不能立刻买入股票)的最大利润。

状态转移

  1. hold[i] :第 i 天持有股票的最大利润。可以从第 i-1 天继续持有,或者从第 i-1 天的冷冻期状态转移过来买入股票。hold[i]=max(hold[i−1],cooldown[i−1]−prices[i])

    hold[i]=max⁡(hold[i−1],cooldown[i−1]−prices[i])\text{hold}[i] = \max(\text{hold}[i-1], \text{cooldown}[i-1] - prices[i])

  2. sell[i] :第 i 天卖出股票的最大利润。可以从第 i-1 天持有股票的状态转移过来卖出股票。sell[i]=hold[i−1]+prices[i]

    sell[i]=hold[i−1]+prices[i]\text{sell}[i] = \text{hold}[i-1] + prices[i]

  3. cooldown[i] :第 i 天处于冷冻期的最大利润。可以从第 i-1 天的卖出状态或者第 i-1 天的冷冻期状态转移过来。cooldown[i]=max(cooldown[i−1],sell[i−1])

    cooldown[i]=max⁡(cooldown[i−1],sell[i−1])\text{cooldown}[i] = \max(\text{cooldown}[i-1], \text{sell}[i-1])

初始条件

  • 第一天之前,假设所有状态都为0,因为没有交易过股票:

    • hold[0] = -prices[0](因为买入股票需要支出)。
    • sell[0] = 0(没有卖出股票)。
    • cooldown[0] = 0(没有冷冻期)。

最终答案

最终的最大利润是 max(sell[n-1], cooldown[n-1]),即最后一天卖出股票的利润或处于冷冻期的利润。

3.代码

我的代码

#include <iostream>
#include <vector>
using namespace std;

int solution(std::vector<int> stocks) {
    // Please write your code here
    vector<int> hold;
    vector<int> sell;
    vector<int> cooldown;
    hold[0] = -stocks[0];
    sell[0] = 0;
    cooldown[0] = 0;
    int n = stocks.size();
    for(int i = 1; i <= n - 1; i++){
        // 从i - 1天继续持有或者从i - 1天冷冻状态过来购买
        hold[i] = max(hold[i - 1], cooldown[i - 1] - stocks[i]);
        // i - 1天持有,i天卖出
        sell[i] = hold[i - 1] + stocks[i];
        // 从i - 1天继续冷冻或者i - 1天卖出
        cooldown[i] = max(cooldown[i - 1], sell[i - 1]);
    }
    //目标状态:最后一天冷冻或者最后一天卖出的最大利润
    return max(cooldown[n - 1], sell[n - 1]);
}

int main() {
    // You can add more test cases here
    std::cout << (solution({1, 2}) == 1) << std::endl;
    std::cout << (solution({2, 1}) == 0) << std::endl;
    std::cout << (solution({1, 2, 3, 0, 2}) == 3) << std::endl;
    std::cout << (solution({2, 3, 4, 5, 6, 7}) == 5) << std::endl;
    std::cout << (solution({1, 6, 2, 7, 13, 2, 8}) == 12) << std::endl;
    return 0;
}

报错:

Not searching for unused variables given on the command line.
-- The C compiler identification is GNU 10.2.1
-- The CXX compiler identification is GNU 10.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /cloudide/workspace/daimalianxi/topic/v2/40/cpp/build
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
[100%] Built target main

vector初始化没有定义长度,所以不可以访问[0]号单元,鉴于本题只访问数组的[i - 1]号单元,也就是考虑第i天时当前的状态,所以可以直接用整形变量,不用数组。

#include <iostream>
#include <vector>
using namespace std;

int solution(std::vector<int> stocks) {
    // Please write your code here
    int hold = -stocks[0];
    int sell = 0;
    int cooldown = 0;
    int n = stocks.size();
    for(int i = 1; i <= n - 1; i++){
        // 从i - 1天继续持有或者从i - 1天冷冻状态过来购买
        int newhold = max(hold, cooldown - stocks[i]);
        // i - 1天持有,i天卖出
        int newsell = hold + stocks[i];
        // 从i - 1天继续冷冻或者i - 1天卖出
        int newcooldown = max(cooldown, sell);
        hold = newhold;
        sell = newsell;
        cooldown = newcooldown;
    }
    //目标状态:最后一天冷冻或者最后一天卖出的最大利润
    return max(cooldown, sell);
}

int main() {
    // You can add more test cases here
    std::cout << (solution({1, 2}) == 1) << std::endl;
    std::cout << (solution({2, 1}) == 0) << std::endl;
    std::cout << (solution({1, 2, 3, 0, 2}) == 3) << std::endl;
    std::cout << (solution({2, 3, 4, 5, 6, 7}) == 5) << std::endl;
    std::cout << (solution({1, 6, 2, 7, 13, 2, 8}) == 12) << std::endl;
    return 0;
}

image.png

参考代码

#include <iostream>
#include <vector>
#include <algorithm>

int solution(std::vector<int> stocks) {
    if (stocks.empty()) return 0;

    int n = stocks.size();

    // 初始化状态
    int hold = -stocks[0];    // 第一天持有股票的利润
    int sell = 0;             // 第一天卖出股票的利润
    int cooldown = 0;         // 第一天冷冻期的利润

    for (int i = 1; i < n; ++i) {
        int newHold = std::max(hold, cooldown - stocks[i]);    // 今天持有股票
        int newSell = hold + stocks[i];                         // 今天卖出股票
        int newCooldown = std::max(cooldown, sell);             // 今天处于冷冻期

        hold = newHold;
        sell = newSell;
        cooldown = newCooldown;
    }

    // 最终利润是卖出股票的利润或冷冻期的利润
    return std::max(sell, cooldown);
}

int main() {
    // You can add more test cases here
    std::cout << (solution({1, 2}) == 1) << std::endl;        // 1
    std::cout << (solution({2, 1}) == 0) << std::endl;        // 0
    std::cout << (solution({1, 2, 3, 0, 2}) == 3) << std::endl; // 3
    std::cout << (solution({2, 3, 4, 5, 6, 7}) == 5) << std::endl; // 5
    std::cout << (solution({1, 6, 2, 7, 13, 2, 8}) == 12) << std::endl; // 12
    return 0;
}