2155. All Divisions With the Highest Score of a Binary Array
Description
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 ofnums
between index0
andi - 1
(inclusive), whilenumsright
has all the elements of nums between indexi
andn - 1
(inclusive).- If
i == 0
,numsleft
is empty, whilenumsright
has all the elements ofnums
. - If
i == n
,numsleft
has all the elements of nums, whilenumsright
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.length
1 <= n <= 105
nums[i]
is either0
or1
.
Solutions
Solution 1: Prefix Sum
We start from \(i = 0\), using two variables \(\textit{l0}\) and \(\textit{r1}\) to respectively record the number of \(1\)s to the left and right of \(i\). Initially, \(\textit{l0} = 0\), while \(\textit{r1} = \sum \textit{nums}\).
We iterate through the array \(\textit{nums}\). For each \(i\), we update \(\textit{l0}\) and \(\textit{r1}\), calculate the current grouping score \(t = \textit{l0} + \textit{r1}\). If \(t\) equals the current maximum grouping score \(\textit{mx}\), then we add \(i\) to the answer array. If \(t\) is greater than \(\textit{mx}\), we update \(\textit{mx}\) to \(t\), clear the answer array, and then add \(i\) to the answer array.
After the iteration ends, we return the answer array.
The time complexity is \(O(n)\), where \(n\) is the length of the array \(\textit{nums}\). The space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|