Problem description:

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:

1
2
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]]

Note:
The length of the given array will not exceed 15.
The range of integer in the given array is [-100,100].
The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.

Solution:

We can use dfs template to solve this question.
The tricky part is to use an unordered_set to store the numbers that already in current layer to avoid duplicate elements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
vector<vector<int>> findSubsequences(vector<int>& nums) {
vector<vector<int>> res;
if(nums.empty()) return res;
vector<int> tmp;
dfs(res, nums, tmp, 0);
return res;
}

void dfs(vector<vector<int>>& res, vector<int>& nums, vector<int>& tmp, int index){
if(tmp.size() >= 2) res.push_back(tmp);

unordered_set<int> dict;
for(int i= index; i< nums.size(); i++){
if(!tmp.empty() && tmp.back()> nums[i] || dict.count(nums[i]))
continue;

tmp.push_back(nums[i]);
dict.insert(nums[i]);
dfs(res, nums, tmp, i+1);
tmp.pop_back();
}
}
};

time complexity: $O(2^n))$
space complexity: $O(n)$
reference:
https://goo.gl/wp2sbS