记录一次菜鸡操作

LeetCode中的一题,虽然结果没问题,但是超时了,下面的操作真的辣眼睛,记录一下犯蠢。

#include "pch.h"
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <numeric>
using namespace std;
static int profit = 0;
static int profit_now = 0;
static int i = 0;
static int flag_buy = 0;
static int flag_sell = 1;
void countprofit(vector<int>& price, int state);

int main(void) {
    vector<int> prices = {1,4,2};
    if (prices.size()<1)
        return 0;
    countprofit(prices, 0);
    profit_now = 0;
    i = 0;
    flag_buy = 0;
    flag_sell = 1;
    countprofit(prices, 1);
    cout << profit << endl;
    return 0;
}
void countprofit(vector<int>& price, int state) {
    if (state == 0) {
        if (flag_buy == 0) {
            profit_now -= price[i];
            flag_buy = 1;
            flag_sell = 0;
            i++;
            if (i < price.size()) {
                //countprofit(price, 1);
                i++;
                if (i < price.size()) {
                    countprofit(price, 0);
                    countprofit(price, 1);
                    countprofit(price, 2);
                }
                else {
                    profit = (profit > profit_now) ? profit : profit_now;
                    i--;
                    return;
                }
                i--;
                //countprofit(price, 2);
                if (flag_sell == 0) {
                    profit_now += price[i];
                    flag_buy = 0;
                    flag_sell = 1;
                    i++;
                    if (i < price.size()) {
                        countprofit(price, 1);
                    }
                    else {

                        profit = (profit > profit_now) ? profit : profit_now;
                        i--;
                        profit_now -= price[i];
                        flag_buy = 1;
                        flag_sell = 0;
                        return;
                    }
                    i--;
                    profit_now -= price[i];
                    flag_buy = 1;
                    flag_sell = 0;
                }
            }
            else {
                profit = (profit > profit_now) ? profit : profit_now;
                i--;
                profit_now += price[i];
                flag_buy = 0;
                flag_sell = 1;
                return;
            }
            i--;
            profit_now += price[i];
            flag_buy = 0;
            flag_sell = 1;
        }
    }
    else if (state == 1) {
        i++;
        if (i < price.size()) {
            countprofit(price, 0);
            countprofit(price, 1);
            countprofit(price, 2);
        }
        else {
            profit = (profit > profit_now) ? profit : profit_now;
            i--;
            return;
        }
        i--;
    }
    else {
        if (flag_sell == 0) {
            profit_now += price[i];
            flag_buy = 0;
            flag_sell = 1;
            i++;
            if (i < price.size()) {
                countprofit(price, 1);
            }
            else {

                profit = (profit > profit_now) ? profit : profit_now;
                i--;
                profit_now -= price[i];
                flag_buy = 1;
                flag_sell = 0;
                return;
            }
            i--;
            profit_now -= price[i];
            flag_buy = 1;
            flag_sell = 0;
        }
    }
    return;
}
#include "pch.h"
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <numeric>
using namespace std;

int main(void) {
    vector<int> prices = {1,4,2};
    int size = prices.size();
    int res = 0;
    vector<int> buy( size,0 );
    vector<int> sell( size,0 );
    buy[0] = -prices[0];
    for (int i = 1; i < size; i++) {
        sell[i] = max(buy[i - 1] + prices[i], sell[i - 1] - prices[i - 1] + prices[i]);
        if (res < sell[i])
            res = sell[i];
        if (i == 1)
            buy[1] = buy[0] + prices[0] - prices[1];
        else
            buy[i] = max(buy[i - 1] + prices[i - 1] - prices[i], sell[i - 2] - prices[i]);
    }
    cout << res;
    return 0;
}
原文地址:https://www.cnblogs.com/lightmonster/p/10580445.html