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.
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:
class Solution {
public List<List<Integer>> findSubsequences(int[] nums) {
int n = nums.length;
List<List<Integer>>[] dp = new List[n];
for (int i = 0; i < n; i ++) {
dp[i] = new ArrayList();
}
for (int i = 0; i < n; i ++) {
List<Integer> list = new ArrayList();
list.add(nums[i]);
dp[i].add(list);
for (int j = 0; j < i; j ++) {
for (List<Integer> l : dp[j]) {
if (nums[i] >= l.get(l.size() - 1)) {
List<Integer> lst = new ArrayList();
lst.addAll(l);
lst.add(nums[i]);
dp[i].add(lst);
}
}
}
}
List<List<Integer>> result = new ArrayList();
Set<List<Integer>> set = new HashSet();
for (int i = 1; i < n; i ++) {
for (List<Integer> l : dp[i]) {
if (l.size() >= 2 && set.add(l)) {
result.add(l);
}
}
}
return result;
}
}