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

g1501_1600.s1590_make_sum_divisible_by_p.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g1501_1600.s1590_make_sum_divisible_by_p

// #Medium #Array #Hash_Table #Prefix_Sum #2023_10_02_Time_464_ms_(100.00%)_Space_58.6_MB_(100.00%)

class Solution {
    fun minSubarray(nums: IntArray, p: Int): Int {
        val hmp = HashMap()
        val n = nums.size
        var target = 0
        var sum = 0
        for (num in nums) {
            target = (num + target) % p
        }
        if (target == 0) {
            return 0
        }
        hmp[0] = -1
        var ans = n
        for (i in 0 until n) {
            sum = (sum + nums[i]) % p
            val key = (sum - target + p) % p
            if (hmp.containsKey(key)) {
                ans = Math.min(ans, i - hmp[key]!!)
            }
            hmp[sum % p] = i
        }
        return if (ans < n) {
            ans
        } else {
            -1
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy