Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
603 B
2
Indexable
class Solution {
public:
    int solve(vector<int>&pri,int i,int pro,bool own){
        if(i==pri.size()) {
            return pro;
        }
        if (own){
            int h=solve(pri,i+1,pro,true);
            int s=solve(pri,i+1,pro + pri[i],false);
            return max(h,s);
        }
        else{
            int h=solve(pri,i+1,pro,false);
            int b=solve(pri,i+1,pro-pri[i],true);
            return max(h,b);
        }
    }

    int maxProfit(vector<int>& prices) {
        int pro=0;
        int res=solve(prices,0,pro,false);
        return res;
    }
};
Leave a Comment