[C++] Clean Code - Iterative


  • 2
    class Solution {
    public:
        vector<vector<int>> findSubsequences(vector<int>& a) {
            set<vector<int>> seqs = {vector<int>(0)};
            for (int i = 0; i < a.size(); i++) {
                vector<vector<int>> built(seqs.size());
                std::copy(seqs.begin(), seqs.end(), built.begin());
                for (auto seq : built) {
                    if (seq.empty() || a[i] >= seq.back()) {
                        seq.push_back(a[i]);
                        seqs.insert(seq);
                    }
                }
            }
            
            vector<vector<int>> res;
            for (auto seq : seqs)
                if (seq.size() > 1) res.push_back(seq);
    
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.