g0401_0500.s0491_increasing_subsequences.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
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
}
}