Function monotone_crescendo::solution::monototone_crescendo_cumulative [−][src]
pub fn monototone_crescendo_cumulative(s: &str) -> i32
Expand description
This is an ingenious solution which was contributed by LeetCode user tarunbisht and translated to Rust by me
Explanation
This solution works by keeping a running total of all ones and all zeroes, where the count of zeroes is initlally contained in the variable flips
.
The variable flips
is then set to the minimum value of the running total of zeroes or running total of ones.
Overall this solution is not only simpler to understand, at least for me, but also requires only O(1) space as opposed to O(N) space.