485. Max Consecutive Ones
Problem Description
You are given an array nums
that consists only of 0s and 1s. Your task is to find the length of the longest subsequence of consecutive 1s in this array. This means you want to find the longest sequence where 1s appear one after another with no 0s between them.
For example:
If the input is nums = [1, 1, 0, 1, 1, 1]
, the consecutive 1s are [1, 1]
, [1, 1, 1]
. Among these, the longest sequence of 1s is [1, 1, 1]
, which has a length of 3. Thus, the output should be 3
.
Intuition
To solve this problem, we perform a single pass through the array, using a counter to keep track of the number of consecutive 1s found at any point. As we iterate over the array, we follow this process:
- When we encounter a
1
, we increase our counter by 1 because we have found another consecutive 1. - When we encounter a
0
, it means the current sequence of consecutive 1s has ended. At this point, we compare our current counter with the maximum length found so far (stored in a separate variable), updating the maximum length if necessary. After that, we reset our counter to zero as we are starting a new sequence from scratch. - After the iteration, we compare and update the maximum length one final time, as the longest sequence of 1s might end at the last element of the array, and no zero would be encountered to trigger the update of the maximum length.
- Finally, we return the maximum length of consecutive 1s found.
This approach works because we are interested in the longest sequence of consecutive 1s, and we're keeping track of the length of the current sequence and the maximum found so far.
Solution Approach
The solution uses a straightforward linear scanning algorithm. It's a single pass through the given array with a time complexity of O(n)
—n
being the number of elements in the input array. No additional data structures are required for this solution, as we can solve the problem using two integer variables: one for the current count of consecutive 1s (cnt
) and another for storing the maximum found so far (ans
).
Here are the details of the solution approach step by step:
-
Initialize two variables
cnt
andans
to0
. Thecnt
variable is used to keep track of the current count of consecutive 1s as we iterate through the array, whileans
is used to store the maximum count of consecutive 1s encountered so far. -
Iterate through each element
v
in the input arraynums
. -
For each element
v
:- If
v
is1
, this means we have encountered a consecutive 1, so we incrementcnt
by1
. - If
v
is not1
(which meansv
is0
), we've reached the end of the current sequence of consecutive 1s, and we need to updateans
with the maximum count so far:ans = max(ans, cnt)
. Then, we resetcnt
to0
as we want to start counting a new sequence of consecutive 1s.
- If
-
After the loop, we perform one last update to
ans
. This step is crucial as the longest sequence of consecutive 1s might end with the last element of the array, so there wouldn't be a0
to trigger the update inside the loop. Therefore, we need to ensureans
also takes into account the count of the last sequence of 1s:ans = max(ans, cnt)
. -
Return
ans
as the final result, which is the maximum number of consecutive 1s found in the array.
The algorithm leverages the simplicity of the problem statement by maintaining a running count and updating the maximum as needed, which avoids the use of additional space and ensures an optimal time complexity.
Ready to land your dream job?
Unlock your dream job with a 2-minute evaluator for a personalized learning plan!
Start EvaluatorExample Walkthrough
Let's consider a small example to understand the solution approach. Suppose the input array is nums = [0, 1, 1, 0, 1, 0, 1, 1, 1, 0]
. We want to find the length of the longest subsequence of consecutive 1's.
Here's how the algorithm works step by step for this example:
-
Initialize
cnt
andans
to0
. At this stage,cnt = 0
andans = 0
. -
Start iterating through each element
v
innums
:v = 0
(1st element):cnt
remains0
as we haven't encountered a1
yet.v = 1
(2nd element): Incrementcnt
to1
. No need to updateans
yet (ans = 0
).v = 1
(3rd element): Incrementcnt
to2
. No need to updateans
yet (ans = 0
).v = 0
(4th element): Sequence of1's
ended. Updateans
tomax(0, 2)
, which is2
. Resetcnt
to0
.
-
Continue the process for remaining elements:
v = 1
(5th element): Incrementcnt
to1
.v = 0
(6th element): Sequence of1's
ended. Updateans
tomax(2, 1)
, which remains2
. Resetcnt
to0
.v = 1
(7th element): Incrementcnt
to1
.v = 1
(8th element): Incrementcnt
to2
.v = 1
(9th element): Incrementcnt
to3
.v = 0
(10th element): Sequence of1's
ended. Updateans
tomax(2, 3)
, which is3
. Resetcnt
to0
.
-
Iteration is complete. Last update
ans
tomax(ans, cnt)
one final time, in case the longest sequence ended with the last element. But in this case, the longest sequence (ans = 3
) had already been updated andcnt
has been reset to0
. Therefore,ans
remains3
. -
Return
ans
, which is3
, the length of the longest subsequence of consecutive 1s found in the array.
By updating ans
each time we reach the end of a consecutive sequence of 1's
and after the final element, the algorithm effectively finds the longest subsequence without the need for additional memory. This example illustrates the algorithm correctly tracking and updating the length of consecutive 1's
sequences.
Solution Implementation
1class Solution:
2 def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
3 # Initialize counters for the current sequence of ones (current_count) and
4 # the maximum sequence found (max_count).
5 current_count = max_count = 0
6
7 # Iterate through each number in the input list.
8 for value in nums:
9 # If the current number is 1, increment the current sequence counter.
10 if value == 1:
11 current_count += 1
12 else:
13 # If the current number is not 1, update the maximum sequence counter
14 # if the current sequence is the longest seen so far.
15 max_count = max(max_count, current_count)
16 # Reset current sequence counter to 0 as the sequence of ones has been broken.
17 current_count = 0
18
19 # After iterating through the list, check once more if the last sequence of ones
20 # is the longest as it could end with the list.
21 return max(max_count, current_count)
22
23# Note: In this implementation, 'nums' is expected to be a list of integers where each integer is 0 or 1.
24
1class Solution {
2 public int findMaxConsecutiveOnes(int[] nums) {
3 // Initialize count of consecutive ones
4 int currentCount = 0;
5 // Initialize the maximum count of consecutive ones
6 int maxCount = 0;
7
8 // Iterate over each element in the array
9 for (int value : nums) {
10 if (value == 1) {
11 // If the current element is 1, increment the current count
12 currentCount++;
13 } else {
14 // If the current element is not 1, update the maxCount if the current count is greater than maxCount
15 maxCount = Math.max(maxCount, currentCount);
16 // Reset the current count to zero
17 currentCount = 0;
18 }
19 }
20
21 // In case the array ends with a sequence of ones, make sure to update the maxCount
22 maxCount = Math.max(currentCount, maxCount);
23
24 // Return the maximum count of consecutive ones found in the array
25 return maxCount;
26 }
27}
28
1#include <vector>
2#include <algorithm> // Include algorithm library to use max function
3
4class Solution {
5public:
6 // Function to find the maximum number of consecutive ones in the vector 'nums'
7 int findMaxConsecutiveOnes(vector<int>& nums) {
8 int currentCount = 0; // Tracks the current sequence length of consecutive ones
9 int maxCount = 0; // Stores the maximum sequence length found
10
11 // Loop over each element in the input vector
12 for (int value : nums) {
13 if (value == 1) {
14 // If the current element is 1, we increment the current sequence length
15 ++currentCount;
16 } else {
17 // If the current element is 0, find the maximum of 'maxCount' and 'currentCount',
18 // then reset 'currentCount' for the next sequence of ones
19 maxCount = std::max(maxCount, currentCount);
20 currentCount = 0;
21 }
22 }
23 // Return the maximum of 'maxCount' and 'currentCount' in case the vector ends with ones
24 return std::max(maxCount, currentCount);
25 }
26};
27
1/**
2 * Finds the maximum number of consecutive 1's in an array.
3 * @param {number[]} nums - The input array of numbers.
4 * @return {number} The length of the longest sequence of consecutive 1's.
5 */
6function findMaxConsecutiveOnes(nums: number[]): number {
7 let maxSequence = 0; // This variable will hold the maximum sequence length.
8 let currentSequence = 0; // This variable will hold the current sequence length.
9
10 // We iterate through each number in the array.
11 for (const num of nums) {
12 if (num === 0) {
13 // If the current number is 0, we update the maxSequence if necessary,
14 // and reset currentSequence since the sequence of 1's is broken.
15 maxSequence = Math.max(maxSequence, currentSequence);
16 currentSequence = 0;
17 } else {
18 // If the current number is 1, we increment the currentSequence counter.
19 currentSequence++;
20 }
21 }
22
23 // After the loop, we check one last time in case the array
24 // ends with a sequence of 1's.
25 return Math.max(maxSequence, currentSequence);
26}
27
Time and Space Complexity
The time complexity of the given code is O(n)
, where n
is the length of the input list nums
. This is because the code iterates over each element of nums
exactly once, performing constant-time operations within the loop.
The space complexity of the code is O(1)
. The only extra space used is for two integer variables cnt
and ans
, which are used to count the current streak of ones and store the maximum streak, respectively. Their space requirement does not vary with the size of the input list, so the space complexity is constant.
Learn more about how to find time and space complexity quickly using problem constraints.
How many times is a tree node visited in a depth first search?
Recommended Readings
LeetCode Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way we
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https algomonster s3 us east 2 amazonaws com recursion jpg You first
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Want a Structured Path to Master System Design Too? Don’t Miss This!