All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0401_0500.s0491_increasing_subsequences.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0401_0500.s0491_increasing_subsequences

// #Medium #Array #Hash_Table #Bit_Manipulation #Backtracking
// #2023_01_03_Time_499_ms_(100.00%)_Space_50.1_MB_(100.00%)

class Solution {
    fun findSubsequences(nums: IntArray): List> {
        if (nums.size == 1) {
            return ArrayList()
        }
        val answer: MutableSet> = HashSet()
        val list: MutableList = ArrayList()
        return ArrayList(backtracking(nums, 0, list, answer))
    }

    private fun backtracking(
        nums: IntArray,
        start: Int,
        currList: MutableList,
        answer: MutableSet>
    ): Set> {
        if (currList.size >= 2) {
            answer.add(ArrayList(currList))
        }
        for (i in start until nums.size) {
            if (currList.isEmpty() || currList[currList.size - 1] <= nums[i]) {
                currList.add(nums[i])
                backtracking(nums, i + 1, currList, answer)
                currList.removeAt(currList.size - 1)
            }
        }
        return answer
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy