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

g1001_1100.s1040_moving_stones_until_consecutive_ii.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.32
Show newest version
package g1001_1100.s1040_moving_stones_until_consecutive_ii

// #Medium #Array #Math #Sorting #Two_Pointers
// #2023_05_26_Time_287_ms_(50.00%)_Space_50.2_MB_(100.00%)

class Solution {
    fun numMovesStonesII(a: IntArray): IntArray? {
        a.sort()
        var i = 0
        val n = a.size
        var low = n
        val high = (a[n - 1] - n + 2 - a[1]).coerceAtLeast(a[n - 2] - a[0] - n + 2)
        for (j in 0 until n) {
            while (a[j] - a[i] >= n) ++i
            low = if (j - i + 1 == n - 1 && a[j] - a[i] == n - 2) low.coerceAtMost(2)
            else low.coerceAtMost(n - (j - i + 1))
        }
        return intArrayOf(low, high)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy