Paste: 2178

Author: cai gou
Mode: c++
Date: Thu, 12 May 2022 01:11:01
Plain Text |
typedef long long ll; 
class Solution {
private:
    vector<ll> res;
    unordered_map<ll, int> hash; 
public:
    vector<long long> maximumEvenSplit(long long finalSum) {
        vector<ll> tmp; 
        if (finalSum % 2 != 0) 
            return {}; 
        dfs(tmp, finalSum, finalSum, 2); 
        return res; 
    }
    bool dfs(vector<ll>& tmp, ll curr, ll finalSum, ll begin) {
        if (hash[curr] == -1) 
            return false; 
        if (!curr) {
            if (!res.size()) {
                res = tmp; 
            }
            return true; 
        }
        if (curr < 0)
            return false; 
        for (ll i = begin; i <= finalSum; i += 2) {
            tmp.push_back(i); 
            curr -= i; 
            if (dfs(tmp, curr, finalSum, i + 2)) {
                hash[curr] = 1; 
                return true; 
            }
            curr += i; 
            tmp.pop_back(); 
        }
        hash[curr] = -1; 
        return false; 
    }
};

New Annotation

Summary:
Author:
Mode:
Body: