zoukankan      html  css  js  c++  java
  • 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<List<Integer>> findSubsequences(int[] nums) {
     3         List<List<Integer>> res = new ArrayList<>();
     4         if(nums == null || nums.length < 2){
     5             return res;
     6         }
     7         
     8         HashSet<List<Integer>> hs = new HashSet<>();
     9         dfs(nums, 0, new ArrayList<Integer>(), hs);
    10         return new ArrayList<List<Integer>>(hs);
    11     }
    12     
    13     private void dfs(int [] nums, int start, List<Integer> item, Set<List<Integer>> hs){
    14         if(item.size() > 1){
    15             hs.add(new ArrayList<Integer>(item));
    16         }
    17         
    18         for(int i = start; i<nums.length; 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<List<Integer>> findSubsequences(int[] nums) {
     3         List<List<Integer>> res = new ArrayList<>();
     4         if(nums == null || nums.length < 2){
     5             return res;
     6         }
     7         
     8         dfs(nums, 0, new ArrayList<Integer>(), res);
     9         return res;
    10     }
    11     
    12     private void dfs(int [] nums, int start, List<Integer> item, List<List<Integer>> res){
    13         if(item.size() > 1){
    14             res.add(new ArrayList<Integer>(item));
    15         }
    16         
    17         HashSet<Integer> visited = new HashSet<Integer>();
    18         for(int i = start; i<nums.length; 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 }
  • 相关阅读:
    Linux入门(四)linux运行环境mysql详细操作及安装phpmyadmin
    Linux入门(五)linux服务器文件远程管理
    Linux入门(六)ubuntu下vim编辑器安装与使用
    windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境)
    Linux入门(三)搭建服务器linux运行环境LAMP/LNMP
    Linux入门(二)Linux基本命令及基本操作
    discuz使用总结
    ThinkPHP3.1快速入门(3)查询语言
    LeetCode 611. 有效三角形的个数(Valid Triangle Number)
    LeetCode 566. 重塑矩阵(Reshape the Matrix)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11947281.html
Copyright © 2011-2022 走看看