g2901_3000.s2938_separate_black_and_white_balls.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2901_3000.s2938_separate_black_and_white_balls
// #Medium #String #Greedy #Two_Pointers #2024_01_03_Time_199_ms_(98.21%)_Space_38.7_MB_(58.93%)
class Solution {
fun minimumSteps(s: String): Long {
var left = 0
var right = s.length - 1
var total: Long = 0
while (left < right) {
while (left < right && s[left] == '0') {
left++
}
while (left < right && s[right] == '1') {
right--
}
if (left < right) {
total += (right - left).toLong()
left++
right--
}
}
return total
}
}