MEDIUM Sliding Window
485. Max Consecutive Ones
๐ Problem
Given a binary array nums, return the maximum number of consecutive 1's in the array.
๐ง Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- โขArray methods (`push`, `pop`, `shift`, `slice`)
- โขObject/Map/Set usage patterns
- โขFunction parameter and return typing
- โขString/array indexing
- โขSet/Map frequency counting
- โขPointer movement invariants
Logical Thinking Concepts
- โขDefine invariants before coding
- โขCheck edge cases first (`[]`, single element, duplicates)
- โขEstimate time/space before implementation
- โขApply Sliding Window reasoning pattern
๐ก Approach
- โ Sliding window to track current consecutive ones
- โ Reset count when encountering 0
- โ Track maximum count seen
- โ Time: O(n), Space: O(1)
๐งญ Prerequisites
๐ ๏ธ Hints & Pitfalls
Hints
- โขSliding window to track current consecutive ones
- โขReset count when encountering 0
- โขTrack maximum count seen
Common Pitfalls
- โขTime: O(n), Space: O(1)
๐งช Test Cases
Hidden tests on submit: 1
Test Case 1
Not run Input:
longestOnes([1,1,0,1,1,1]); Expected:
3 Test Case 2
Not run Input:
longestOnes([1,0,1,1,0,1]); Expected:
2 Test Case 3
Not run Input:
longestOnes([0,0,0,0]); Expected:
0 ๐ Code Editor
๐ค Output