Java solution beats 100%.


  • 0
    S
    public class Solution {
        public List<List<Integer>> findSubsequences(int[] nums) {
            Set<List<Integer>> result = new HashSet<>();
            
            List<List<List<Integer>>> temp = new ArrayList<>();
            for(int value: nums) {
                List<Integer> arrayList = new ArrayList<>();
                arrayList.add(value);
                List<List<Integer>> parent = new ArrayList<>();
                parent.add(arrayList);
                temp.add(parent);
            }
            
    
            
            for(int i=1; i<nums.length; i++) {
                int j = 0;
                
                while(j<i) {
                    if(nums[i] >= nums[j]) {
                        List<List<Integer>> listJ = temp.get(j);
                        List<List<Integer>> listI = temp.get(i);
                        for(List<Integer> value: listJ) {
                            List<Integer> newOne = new ArrayList<>(value);
                            newOne.add(nums[i]);
                            listI.add(newOne);
                            result.add(newOne);
                        }
                    }
                    j++;
                }
            }
            
            List<List<Integer>> finalResult = new ArrayList<>();
            for(List<Integer> value: result) {
                finalResult.add(value);
            }
            
            return finalResult;
            
        }
    }

  • 0
    S
    This post is deleted!

  • 0
    S

    Sorry, I had it mistakenly tagged as O(N^2) solution. It is definitely not O(N^2) because in worst case we have to evaluate all the subsets, so it is exponential


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.