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

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

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

// #Hard #Array #Greedy #2022_11_12_Time_201_ms_(100.00%)_Space_36.5_MB_(100.00%)

class Solution {
    fun minPatches(nums: IntArray, n: Int): Int {
        var res = 0
        var sum: Long = 0
        var i = 0
        while (sum < n) {
            // required number
            val req = sum + 1
            if (i < nums.size && nums[i] <= req) {
                sum += nums[i++].toLong()
            } else {
                sum += req
                res++
            }
        }
        return res
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy