clueless coding // TODO: be smarter

LeetCode Factor Combinations: CPP Solution



TL;DR Code Solution


void factorize(vector<vector<int>>& res, vector<int> curr, int n, int start) {
    if (n == 1) {
        if (curr.size() > 1) {
            res.push_back(curr);
        }
        return;
    }

    for (int i = start; i <= n; i++) {
        if (n % i == 0) {
            curr.push_back(i);
            factorize(res, curr, n / i, i);
            curr.pop_back();
        }
    }
}

vector<vector<int>> getFactors(int n) {
    vector<vector<int>> res;
    vector<int> curr;
    factorize(res, curr, n, 2);
    return res;
}