Last active
November 6, 2020 15:47
-
-
Save rphuber/40d8e1cf7814e2bddd8aa7904613a321 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a binary array, find the maximum number of consecutive 1s in the array | |
Input: [1, 1, 0, 1, 1, 1] | |
Output: 3 | |
Note: | |
- the input array will contain 0 and 1 | |
- the length of the input array is a positive integer and will not exceed 10,000 | |
*/ | |
func findMaxConsecutiveOnes(nums []int) int { | |
answer := 0 | |
current := 0 | |
for _, val := range nums { | |
if val == 1 { | |
current++ | |
} else { | |
current = 0 | |
} | |
if current > answer { | |
answer = current | |
} | |
} | |
return answer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment