Given a binary array nums, return the maximum number of consecutive 1's in the array.
Example 1:
Input: nums = [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
Example 2:
Input: nums = [1,0,1,1,0,1] Output: 2
Constraints:
1 <= nums.length <= 105nums[i] is either 0 or 1.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the longest stretch of ones, the brute force strategy is to look at every single possible group of numbers in our list, one by one. For each group, we'll check if it's made up of only ones, and then we'll remember the length of the longest one we've found so far.
Here's how the algorithm would work step-by-step:
def find_max_consecutive_ones_brute_force(numbers_list):
maximum_length_of_ones = 0
# Step 1: Consider every possible starting point in the list.
for start_index in range(len(numbers_list)):
# Step 2: From each start, consider every possible end point to form a segment.
for end_index in range(start_index, len(numbers_list)):
segment = numbers_list[start_index:end_index + 1]
all_are_ones = True
# Step 3 & 4: Check if all numbers in the current segment are ones.
for number_in_segment in segment:
if number_in_segment != 1:
all_are_ones = False
break
# Step 5, 6, & 7: If the segment is all ones, update the maximum length found so far.
if all_are_ones:
current_segment_length = len(segment)
if current_segment_length > maximum_length_of_ones:
maximum_length_of_ones = current_segment_length
# Step 8: After checking all segments, we have the final answer.
return maximum_length_of_onesThe best way to solve this is to simply walk through the list of numbers one by one, keeping a running count of the consecutive ones we see. We'll also remember the highest count we've found so far, updating it whenever our current streak of ones gets bigger.
Here's how the algorithm would work step-by-step:
def find_max_consecutive_ones(list_of_numbers):
current_streak_of_ones = 0
longest_streak_so_far = 0
for number in list_of_numbers:
if number == 1:
current_streak_of_ones += 1
else:
# A zero breaks the sequence, so we must reset the current streak count.
current_streak_of_ones = 0
# We need to check if the current streak is the new maximum after every number.
if current_streak_of_ones > longest_streak_so_far:
longest_streak_so_far = current_streak_of_ones
# This ensures that a streak ending at the last element is also considered for the maximum.
return longest_streak_so_far| Case | How to Handle |
|---|---|
| An empty input array | The loop will not execute, and the initial maximum count of 0 will be correctly returned. |
| A null input array | The solution should include a check to handle null input gracefully, often by returning 0 or throwing an exception. |
| An array containing only zeros | The current count of ones will never increment, resulting in the correct maximum of 0. |
| An array containing only ones | The current count will increment for every element, and the final maximum will correctly equal the array's length. |
| An array with a single element, which is a 1 | The algorithm will correctly count one 1 and return a maximum of 1. |
| The longest sequence of ones is at the very beginning of the array | The maximum count is updated correctly as soon as the sequence is broken by a zero or the array ends. |
| The longest sequence of ones is at the very end of the array | A final check after the loop ensures the last computed sequence is compared against the overall maximum. |
| A very large input array near memory or time limits | A single-pass O(N) time and O(1) space solution handles large inputs efficiently without memory issues. |