LeetCode 491. Increasing Subsequences

原题链接在这里:https://leetcode.com/problems/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]]

Note:

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

题解:

The DFS needs the state, current start index, current item.

When item size is already >= 2, then add a copy to res.

Otherwise, iterate from start, if nums[i] >= last number in item, then add it to item.

When backtracking, remove the last added number.

Time Complexity: exponential.

Space: O(n). n = nums.length.

AC Java:

 1 class Solution {
 2     public List> findSubsequences(int[] nums) {
 3         List> res = new ArrayList<>();
 4         if(nums == null || nums.length < 2){
 5             return res;
 6         }
 7         
 8         HashSet> hs = new HashSet<>();
 9         dfs(nums, 0, new ArrayList(), hs);
10         return new ArrayList>(hs);
11     }
12     
13     private void dfs(int [] nums, int start, List item, Set> hs){
14         if(item.size() > 1){
15             hs.add(new ArrayList(item));
16         }
17         
18         for(int i = start; i){
19             if(item.size() == 0 || item.get(item.size()-1) <= nums[i]){
20                 item.add(nums[i]);
21                 dfs(nums, i+1, item, hs);
22                 item.remove(item.size()-1);
23             }
24         }
25     }
26 }

Another way to avoid the duplicate is to record visited item on each level of DFS.

For the same level, if we see a previous visited number.

e.g. 4,6,7,7. when item = 4,6. First time visited 7, 7 is added to set. set = [6, 7]. item = 4,6,7. set is on this level of DFS.

Later it is removed from item, but it is still in the set. Thus when encountering the 2nd 7, it would skip.

But item = 4,6,7,7 still is added to res. That is because the second 7 is added to set on next level of DFS.

Time Complexity: exponential.

Space: O(n). n = nums.length.

AC Java:

 1 class Solution {
 2     public List> findSubsequences(int[] nums) {
 3         List> res = new ArrayList<>();
 4         if(nums == null || nums.length < 2){
 5             return res;
 6         }
 7         
 8         dfs(nums, 0, new ArrayList(), res);
 9         return res;
10     }
11     
12     private void dfs(int [] nums, int start, List item, List> res){
13         if(item.size() > 1){
14             res.add(new ArrayList(item));
15         }
16         
17         HashSet visited = new HashSet();
18         for(int i = start; i){
19             if(visited.contains(nums[i])){
20                 continue;
21             }
22             
23             if(item.size() == 0 || item.get(item.size()-1) <= nums[i]){
24                 visited.add(nums[i]);
25                 item.add(nums[i]);
26                 dfs(nums, i+1, item, res);
27                 item.remove(item.size()-1);
28             }
29         }
30     }
31 }

 

你可能感兴趣的:(LeetCode 491. Increasing Subsequences)