leetcode491. Increasing subsequence/dfs

Subject: 491. Increasing Subsequence

Given an integer array, your task is to find all increasing subsequences of the array, and the length of the increasing subsequence is at least 2.

Example:

输入: [4, 6, 7, 7]
输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]

Description:

  • The length of the given array will not exceed 15.
  • The range of integers in the array is [-100,100].
  • A given array may contain repeated numbers, and equal numbers should be considered as an increase.

Source: LeetCode
Link: https://leetcode-cn.com/problems/increasing-subsequences
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Basic idea: dfs

Here to explain: the title requires an increasing subsequence, the only difference between a subsequence and a substring is: the subsequence can be discontinuous. It is obvious here: the subsequence should maintain the relative order of each number in the original array.

Deduplication with the help of set:

class Solution {
    
    
public:
    set<vector<int>> res;
    vector<vector<int>> findSubsequences(vector<int>& nums) {
    
    
        vector<vector<int>> vres;
        
        if(nums.size() < 2)
            return vres;
        //子序列要保持相对顺序
        vector<int> cur;
        dfs(nums, 0, cur);
        
        for(auto r : res){
    
    
            vres.push_back(r);
        }
        return vres;
    }
    void dfs(vector<int>& nums, int pos, vector<int> cur){
    
    
        if(cur.size() > 1)
            res.insert(cur);
        for(int i = pos; i < nums.size(); ++i){
    
    
            if(cur.size() == 0 || nums[i] >= cur.back()){
    
    
                cur.push_back(nums[i]);
                dfs(nums, i + 1, cur);
                cur.pop_back();
            }
        }
    }
};

Pruning and de-duplication in the process of dfs: It is
realized by saving the popped elements when backtracking, to ensure that the popped elements will not enter the current array again.

class Solution {
    
    
public:
    vector<vector<int>> res;
    vector<vector<int>> findSubsequences(vector<int>& nums) {
    
    
        if(nums.size() < 2)
            return res;
        vector<int> cur;
        dfs(nums, 0, cur);
        return res;
    }
    void dfs(vector<int>& nums, int pos, vector<int> cur){
    
    
        if(cur.size() > 1)
            res.push_back(cur);
        unordered_set<int> help;
        for(int i = pos; i < nums.size(); ++i){
    
    
            
            if((cur.size() == 0 || nums[i] >= cur.back()) && help.find(nums[i]) == help.end()){
    
    
                cur.push_back(nums[i]);
                dfs(nums, i + 1, cur);
                cur.pop_back();
                //去掉重复的元素
                help.insert(nums[i]);
            }
            
        }
    }
};

Guess you like

Origin blog.csdn.net/qq_31672701/article/details/108220207