491. Increasing Subsequences
Medium
42679FavoriteShare
Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2.
Example:
Input: [4, 6, 7, 7]
Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]
class Solution {
public:
vector<vector<int>> findSubsequences(vector<int>& nums) {
vector<int> tmp;
set<vector<int>> Res;
DFS(nums,tmp,Res,0);
vector<vector<int>> Result(Res.begin(),Res.end());
return Result;
}
void DFS(const vector<int>& nums,vector<int>& tmp,set<vector<int>>& Res,int k){
if(tmp.size() >= 2){
Res.insert(tmp);
}
for(int i = k;i < nums.size();i ++){
if(tmp.empty() || nums[i] >= tmp.back()){
tmp.push_back(nums[i]);
DFS(nums,tmp,Res,i + 1);
tmp.pop_back();
}
}
}
};