//未AC,19/44,超时
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
class Solution {
public:
int helper(vector<int>& prices, int d, int states, int d_max, int res, int fee, int& cost) {
int res1, res2, states1, states2, r_max1, r_max2;
if (d == d_max) {
cost = max(cost, res);
return 0;
}
if (states == 1) {//手上持有股票
res1 = res + 0; //继续持有
states1 = 1;
res2 = res + prices[d] - fee; //出售
states2 = 0;
//d++;
//if (d == d_max) {
//return r_max;
// }
helper(prices, d+1, states1, d_max, res1, fee, cost);
helper(prices, d+1, states2, d_max, res2, fee, cost);
}
if (states == 0) {//手上没有股票
res1 = res + 0;
states1 = 0;
res2 = res - prices[d];//买入
states2 = 1;
//d++;
//if (d == d_max) return r_max;
helper(prices, d+1, states2, d_max, res2, fee, cost);
helper(prices, d+1, states1, d_max, res1, fee, cost);
//res2 = helper(prices, d, states2, d_max, res2, fee);
}
return 0;
}
int maxProfit(vector<int>& prices, int fee) {
int cost = -1000000;
int N = prices.size();
int res;
helper(prices, 0, 0, N, 0, fee, cost);
//sort(cost.begin(), cost.end());
return cost;
}
};
int main() {
Solution sol;
vector<int> prices = { 36,1,5,4,90,2,6};
int fee = 2;
cout << sol.maxProfit(prices, fee) << endl;
return 0;
}