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

g0901_1000.s0948_bag_of_tokens.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0901_1000.s0948_bag_of_tokens

// #Medium #Array #Sorting #Greedy #Two_Pointers
// #2023_05_01_Time_161_ms_(100.00%)_Space_37.4_MB_(33.33%)

@Suppress("NAME_SHADOWING")
class Solution {
    fun bagOfTokensScore(tokens: IntArray, power: Int): Int {
        var power = power
        tokens.sort()
        var score = 0
        var l = 0
        var r = tokens.size - 1
        while (l <= r) {
            if (tokens[l] <= power) {
                power -= tokens[l]
                score++
                l++
            } else {
                if (r - l == 0) {
                    return score
                }
                if (score >= 1) {
                    power += tokens[r]
                    score--
                    r--
                } else {
                    return 0
                }
            }
        }
        return score
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy