You are given a 0-indexed binary array nums of length n. nums can be divided at index i (where 0 <= i <= n) into two arrays (possibly empty) numsleft and numsright:
numsleft has all the elements of nums between index 0 and i - 1 (inclusive), while numsright has all the elements of nums between index i and n - 1 (inclusive).i == 0, numsleft is empty, while numsright has all the elements of nums.i == n, numsleft has all the elements of nums, while numsright is empty.The division score of an index i is the sum of the number of 0's in numsleft and the number of 1's in numsright.
Return all distinct indices that have the highest possible division score. You may return the answer in any order.
Example 1:
Input: nums = [0,0,1,0] Output: [2,4] Explanation: Division at index - 0: numsleft is []. numsright is [0,0,1,0]. The score is 0 + 1 = 1. - 1: numsleft is [0]. numsright is [0,1,0]. The score is 1 + 1 = 2. - 2: numsleft is [0,0]. numsright is [1,0]. The score is 2 + 1 = 3. - 3: numsleft is [0,0,1]. numsright is [0]. The score is 2 + 0 = 2. - 4: numsleft is [0,0,1,0]. numsright is []. The score is 3 + 0 = 3. Indices 2 and 4 both have the highest possible division score 3. Note the answer [4,2] would also be accepted.
Example 2:
Input: nums = [0,0,0] Output: [3] Explanation: Division at index - 0: numsleft is []. numsright is [0,0,0]. The score is 0 + 0 = 0. - 1: numsleft is [0]. numsright is [0,0]. The score is 1 + 0 = 1. - 2: numsleft is [0,0]. numsright is [0]. The score is 2 + 0 = 2. - 3: numsleft is [0,0,0]. numsright is []. The score is 3 + 0 = 3. Only index 3 has the highest possible division score 3.
Example 3:
Input: nums = [1,1] Output: [0] Explanation: Division at index - 0: numsleft is []. numsright is [1,1]. The score is 0 + 2 = 2. - 1: numsleft is [1]. numsright is [1]. The score is 0 + 1 = 1. - 2: numsleft is [1,1]. numsright is []. The score is 0 + 0 = 0. Only index 0 has the highest possible division score 2.
Constraints:
n == nums.length1 <= n <= 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:
We need to find the best place to split a series of zeros and ones. The brute force method involves checking every possible split to see which one gives us the highest score. We calculate the score for each possible split and keep track of the best ones.
Here's how the algorithm would work step-by-step:
def all_divisions_with_highest_score_brute_force(numbers):
maximum_score = -1
indices_with_maximum_score = []
for division_index in range(len(numbers) + 1):
# Initialize counts for zeros on the left and ones on the right
zeros_on_left = 0
ones_on_right = 0
# Count zeros to the left of the division
for index_left in range(division_index):
if numbers[index_left] == 0:
zeros_on_left += 1
# Count ones to the right of the division
for index_right in range(division_index, len(numbers)):
if numbers[index_right] == 1:
ones_on_right += 1
current_score = zeros_on_left + ones_on_right
# Update the maximum score and indices if necessary
if current_score > maximum_score:
# Found a new maximum, so reset
maximum_score = current_score
indices_with_maximum_score = [division_index]
elif current_score == maximum_score:
# Found another index with the maximum, so add
indices_with_maximum_score.append(division_index)
return indices_with_maximum_scoreThe key to efficiently finding the best division point in the binary array is to avoid repeatedly counting zeros and ones for each potential division. We can achieve this by calculating the prefix sums of both zeros and ones, and then using these to compute the score for each division point very quickly.
Here's how the algorithm would work step-by-step:
def find_best_division(binary_array):
total_ones = sum(binary_array)
max_score = -1
indices_with_max_score = []
zeros_count = 0
ones_count = 0
for i in range(len(binary_array) + 1):
# Calculates the score for the current division.
score = zeros_count + (total_ones - ones_count)
if score > max_score:
# A new higher score found.
max_score = score
indices_with_max_score = [i]
elif score == max_score:
indices_with_max_score.append(i)
# Avoid index out of bounds error at the last iteration
if i < len(binary_array):
if binary_array[i] == 0:
#Update the count of zeros seen so far.
zeros_count += 1
else:
ones_count += 1
return indices_with_max_score| Case | How to Handle |
|---|---|
| Null or empty input array | Return an empty list because no divisions can be evaluated. |
| Array containing only zeros | All divisions will have the same score (the array length), so return a list of all indices. |
| Array containing only ones | The scores increase linearly from 0 to n, so only index n will be returned. |
| Large input array exceeding memory constraints | Optimize the solution to use O(1) extra space, avoiding storing large intermediate arrays. |
| Array with alternating 0s and 1s | Carefully consider how the prefix sums of 0s and 1s interact to correctly compute the scores. |
| Input array is very long, leading to potential integer overflow issues when calculating score | Use a 64-bit integer type to store scores, or normalize them if needed. |
| Multiple divisions with the same highest score | The algorithm needs to collect and return all indices where the highest score is achieved. |
| Array with a single element | Return 0 if the element is 1 and 1 if the element is 0. |