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

g0301_0400.s0368_largest_divisible_subset.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0301_0400.s0368_largest_divisible_subset

// #Medium #Array #Dynamic_Programming #Math #Sorting
// #2022_11_19_Time_412_ms_(73.33%)_Space_40.8_MB_(80.00%)

class Solution {
    fun largestDivisibleSubset(nums: IntArray): List {
        val n = nums.size
        val count = IntArray(n)
        val pre = IntArray(n)
        nums.sort()
        var max = 0
        var index = -1
        for (i in 0 until n) {
            count[i] = 1
            pre[i] = -1
            for (j in i - 1 downTo 0) {
                if (nums[i] % nums[j] == 0 && 1 + count[j] > count[i]) {
                    count[i] = count[j] + 1
                    pre[i] = j
                }
            }
            if (count[i] > max) {
                max = count[i]
                index = i
            }
        }
        val res: MutableList = ArrayList()
        while (index != -1) {
            res.add(nums[index])
            index = pre[index]
        }
        return res
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy