Increasing Subsequences

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

 

Constraints:


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;
    }
}