927. Three Equal Parts
Description
You are given an array arr
which consists of only zeros and ones, divide the array into three non-empty parts such that all of these parts represent the same binary value.
If it is possible, return any [i, j]
with i + 1 < j
, such that:
arr[0], arr[1], ..., arr[i]
is the first part,arr[i + 1], arr[i + 2], ..., arr[j - 1]
is the second part, andarr[j], arr[j + 1], ..., arr[arr.length - 1]
is the third part.- All three parts have equal binary values.
If it is not possible, return [-1, -1]
.
Note that the entire part is used when considering what binary value it represents. For example, [1,1,0]
represents 6
in decimal, not 3
. Also, leading zeros are allowed, so [0,1,1]
and [1,1]
represent the same value.
Example 1:
Input: arr = [1,0,1,0,1] Output: [0,3]
Example 2:
Input: arr = [1,1,0,1,1] Output: [-1,-1]
Example 3:
Input: arr = [1,1,0,0,1] Output: [0,2]
Constraints:
3 <= arr.length <= 3 * 104
arr[i]
is0
or1
Solutions
Solution 1: Counting + Three Pointers
We denote the length of the array as \(n\), and the number of '1's in the array as \(cnt\).
Obviously, \(cnt\) must be a multiple of \(3\), otherwise the array cannot be divided into three equal parts, and we can return \([-1, -1]\) in advance. If \(cnt\) is \(0\), it means that all elements in the array are '0', and we can directly return \([0, n - 1]\).
We divide \(cnt\) by \(3\) to get the number of '1's in each part, and then find the position of the first '1' in each part in the array arr
(refer to the \(find(x)\) function in the following code), denoted as \(i\), \(j\), \(k\) respectively.
0 1 1 0 0 0 1 1 0 0 0 0 0 1 1 0 0
^ ^ ^
i j k
Then we start from \(i\), \(j\), \(k\) and traverse each part at the same time, check whether the corresponding values of the three parts are equal. If they are, continue to traverse until \(k\) reaches the end of arr
.
0 1 1 0 0 0 1 1 0 0 0 0 0 1 1 0 0
^ ^ ^
i j k
At the end of the traversal, if \(k=n\), it means that it satisfies the three equal parts, and we return \([i - 1, j]\) as the answer, otherwise return \([-1, -1]\).
The time complexity is \(O(n)\), where \(n\) is the length of arr
. The space complexity is \(O(1)\).
Similar problems:
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 24 25 26 27 28 29 30 31 32 33 34 35 |
|
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 22 23 24 25 26 27 28 29 30 31 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|