19. Longest Streak of Ones

EasyArray

A fitness tracker records each day as 1 if the user hit their step goal and 0 if they missed it. The app shows the user their best streak: the greatest number of goal days in a row.

Write a method longestOnesRun that takes the array nums of 0s and 1s and returns the length of the longest run of consecutive 1s. If there are no 1s, return 0.

Example 1

Input:
nums = [1,1,0,1,1,1]
Output:
3
Explanation:

The first streak is 2 days long and the second is 3 days long, so the best is 3.

Example 2

Input:
nums = [0,0,0]
Output:
0
Explanation:

There are no goal days at all.

Constraints

1 ≤ nums.length ≤ 1000

nums[i] is either 0 or 1

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…