Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented Jun 8, 2021

Resolves #59

Algorithm:

Points:

  • Putting a 0 means that we are connecting 2 blocks of 1's.
  • When we are inside a block of 1's, that means that we are either on the left or the united-right side of the block.
  • When we hit a 0, that means that we move on the right block. The united block assumes the length left+1, we increment it with coming 1's if present after the 0. The left's length becomes 0 (It becomes the left of a new possible block).
  • Each time, we check if the length of the united block is better than our previous best.

From these points, the algorithm follows.

For example, in the array [1,1,0,1,1], we hit the 0. Here the united block assumes the length left+1 = 3. After encountering the other 1's, it gets the length 5.

@ErdemT09 ErdemT09 marked this pull request as ready for review June 8, 2021 17:59

public class MaxConsecutiveOnes2 {

public int findMaxConsecutiveOnes(int[] nums) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great:
image

Copy link
Collaborator

@altay9 altay9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, quite a clean explanation and self-expressive code.
Thanks, Erdem.

@ErdemT09 ErdemT09 merged commit 5c3d6e8 into master Jun 8, 2021
@ErdemT09 ErdemT09 deleted the 487-Max-Consecutive-Ones-II branch June 8, 2021 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

487. Max Consecutive Ones II

3 participants