Given a binary array nums (containing only 0s and 1s), return the maximum number of consecutive 1s in the array.
We use analytics and advertising cookies to understand how the site is used and whether our ads on Facebook and Instagram work. They are set only if you accept. See our Privacy Policy for details.
Given a binary array nums (containing only 0s and 1s), return the maximum number of consecutive 1s in the array.
An easy sliding window problem, graded against 7 test cases (4 of them hidden).
A moving window over a contiguous run, expanding and contracting to hold an invariant.
Reach for it when you see: "Longest/shortest contiguous subarray or substring satisfying X."
More Sliding Window problems →Scan left to right keeping two counters: the length of the current streak of 1s and the best streak seen so far. A 1 extends the current streak (and may update the best); a 0 resets the current streak to zero.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.