zoukankan      html  css  js  c++  java
  • 491. Increasing Subsequences

    package LeetCode_491
    
    /**
     * 491. Increasing Subsequences
     * https://leetcode.com/problems/increasing-subsequences/description/
     * 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:
    The length of the given array will not exceed 15.
    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.
     * */
    class Solution {
        /*
        * solution: backtracking, Time complexity:O(2^n), Space complexity:O(n)
        * */
    
        private val result = ArrayList<ArrayList<Int>>()
    
        fun findSubsequences(nums: IntArray): List<List<Int>>? {
            val set = HashSet<ArrayList<Int>>()
            dfs(0, ArrayList(), nums, set)
            return result
        }
    
        private fun dfs(index: Int, cur: ArrayList<Int>, nums: IntArray, set:HashSet<ArrayList<Int>>) {
            val temp = ArrayList<Int>(cur)
            if (set.contains(temp)){
                return
            }
            if (temp.size >= 2) {
                result.add(temp)
            }
            set.add(temp)
            for (i in index until nums.size) {
                //because need increasing, so if last num larger than current one, skip it
                if (cur.isNotEmpty() && cur.last() > nums[i]){
                    continue
                }
                cur.add(nums[i])
                dfs(i + 1, cur, nums,set)
                cur.removeAt(cur.size - 1)
            }
        }
    }
  • 相关阅读:
    ansible的管理与剧本
    条件随机场入门(二) 条件随机场的模型表示
    条件随机场入门(一) 概率无向图模型
    隐马尔科夫模型
    高斯混合模型 GMM
    K-Means 算法
    EM 算法
    Sequential Minimal Optimization (SMO) 算法
    LinkedIn文本分析平台:主题挖掘的四大技术步骤
    SVM 核方法
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13054387.html
Copyright © 2011-2022 走看看