LeetCode Entry
1422. Maximum Score After Splitting a String
Max left zeros + right ones in 01-array
1422. Maximum Score After Splitting a String easy
blog post
substack

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/447
Problem TLDR
Max left_zeros + right_ones in 01-array
Intuition
We can count ones and then scan from the beginning modifying the ones and zeros counts. After some retrospect, we can do this with score variable.
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\), dropLast(1) creates the second list, but we can just use pointers or
asSequence
Code
fun maxScore(s: String): Int {
var score = s.count { it == '1' }
return s.dropLast(1).maxOf {
if (it == '0') ++score else --score
}
}