LeetCode Entry
2914. Minimum Number of Changes to Make Binary String Beautiful
Min flips to make even islands of zeros and ones
2914. Minimum Number of Changes to Make Binary String Beautiful medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1546
Problem TLDR
Min flips to make even islands of zeros and ones
Intuition
Greedy workds: consider each pair, if it doesn’t matches, we flip a bit. Doesn’t mutter how we flip it.
Approach
- Kotlin: chunked
- Rust: chunks
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun minChanges(s: String) =
s.chunked(2).sumOf{abs(it[0]-it[1])}
pub fn min_changes(s: String) -> i32 {
s.as_bytes().chunks(2).map(|c| (c[0] != c[1]) as i32).sum()
}
Comments