Skip to content

Instantly share code, notes, and snippets.

@rphuber
Last active November 6, 2020 15:47
Show Gist options
  • Save rphuber/40d8e1cf7814e2bddd8aa7904613a321 to your computer and use it in GitHub Desktop.
Save rphuber/40d8e1cf7814e2bddd8aa7904613a321 to your computer and use it in GitHub Desktop.
/*
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