Show problems as
Classic problem #485EasyArray

Max Consecutive Ones

Real-world version: Longest Online Panel Run
Tracked in this browser only
Read Max Consecutive Ones on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A solar farm polls its panels along one long row and hands you the reply as status, listed in the order the panels sit on the row. status[i] is 1 when panel i answered and 0 when it did not.

A crew wants to know the widest stretch of neighbouring panels that all answered. Return the number of panels in the longest stretch that contains no silent panel, or 0 when no panel answered at all.

Examples

Example 1

Input
status = [1, 1, 1, 0, 1, 1]
Output
3

The first three panels answer in a row. The pair at the end is only two wide.

Example 2

Input
status = [0, 1, 0, 0, 1, 1, 1, 1]
Output
4

The stretch runs to the end of the row, so it still counts as four panels.

Example 3

Input
status = [0, 0]
Output
0

Nothing answered, so there is no stretch to measure.

Constraints

  • 1 <= status.length <= 10^5
  • status[i] is either 0 or 1.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def longest_online_run(status: list[int]) -> int:
Java
public int longestOnlineRun(int[] status)