Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Wednesday, October 25, 2017

485. Max Consecutive Ones

First blog!
Let's start with this short interesting one.

Problem:
Find the max length of consecutive ones in a "01" vector.

Thinking process:
1. 0s divide 1s. So ++count of 1s while num == 1, reset it to 0 while num == 0. Update the ans to max each time.
2. To decrease comparison times: If 1s are more frequent than 0s, we can update ans whenever we meet a 0; but need one more update after traversing the vector.
3. On the contrary, If 0s are more frequent, update ans while num == 1.
4. Corner case: vector is empty => 0