Given an array `A` of `0`s and `1`s, divide the array into 3 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:

  • A[0], A[1], ..., A[i] is the first part;
  • A[i+1], A[i+2], ..., A[j-1] is the second part, and
  • A[j], A[j+1], ..., A[A.length - 1] is the third part.
  • All three parts have equal binary value.

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: [1,0,1,0,1]
Output: [0,3]

Example 2:

Input: [1,1,0,1,1]
Output: [-1,-1]

Note:

  1. 3 <= A.length <= 30000
  2. A[i] == 0 or A[i] == 1

这道题给了我们一个只有0和1的数组,让将其分为三段,使得每一段组成的二进制的数相同,注意数组左边的数字是高位,且开头可能会存在多余的0。最开始博主使用的方法是最直接的暴力方法,遍历所有的分成三段的可能,然后将每一段的二进制都计算出来比较是否相等,结果毫不意外的超时了,丝毫没有尊重这道题的 Hard 标签。仔细分析以下题目,既然要分成三段,且二进制数字相等,那么这三个二进制数中1的个数一定是相等的,因为转十进制的时候,只有1才会累加,而由于开头0的存在,所以0的个数不一定相同。那么1的个数就是突破口了,先遍历整个数组,统计出1的个数。假如数组中没有1的话,那就简单了,随便分三段都行了,因为都是0,所以返回 {0, n-1} 就行。再来想一下,假如1的个数不是3的个数,就绝无可能分为相等值的三段,直接返回 {-1,-1} 即可。假如个数是3的倍数,也不表示一定能成功分为3段,但如果能分的话,每段的1的个数一定是 cntOne/3,现在从末尾开始,找出正好有 cntOnt/3 个的位置 idxThird,此时虽然范围 [idxThird, n-1] 不一定是第三段,因为前面可能有0,但如果能成功分的话,其1的个数及位置一定是正确的。此时要做的是,从开头0开始,略去连续0,然后和 [idxThird, n-1] 区间对比,一旦有不一样的位置,说明无法正确分,返回 {-1,-1}。若能找到相同的一段话,说明此时第一段和第三段存在了,再来检测第二段,此时中间段的1的个数已经确定为 cntOne/3 了,只需要再确定其位置是否一样就可以了,其实主要比的是1的位置,因为开头连续0可以略去,就算每个区间末尾还有0,这些0是可以移动到下一个区间的开头的,从而可以保证对应的1的位置还是一样的,参见代码如下:


解法一:

class Solution {
public:
vector<int> threeEqualParts(vector<int>& A) {
int cntOne = 0, n = A.size();
for (int num : A) {
if (num == 1) ++cntOne;
}
if (cntOne == 0) return {0, n - 1};
if (cntOne % 3 != 0) return {-1, -1};
int idxThird = 0, cnt = 0;
for (int i = n - 1; i >= 0; --i) {
if (A[i] == 0) continue;
++cnt;
if (cnt == cntOne / 3) {
idxThird = i;
break;
}
}
int idx1 = helper(A, 0, idxThird);
if (idx1 < 0) return {-1, -1};
int idx2 = helper(A, idx1 + 1, idxThird);
if (idx2 < 0) return {-1, -1};
return {idx1, idx2 + 1};
}
int helper(vector<int>& A, int left, int right) {
while (A[left] == 0) ++left;
while (right < A.size()) {
if (A[left] != A[right]) return -1;
++left; ++right;
}
return left - 1;
}
};

再来看一种比较类似的方法,开始的操作还是一样的,统计1的个数 cntOne,然后计算出每段的1的个数 k=cntOne/3,再用三个变量 start, mid, end 来标记每段的第一个1的位置,因为想要跳过开头的连续0。再用另一个变量 cnt 来重新在遍历的过程中统计1的个数,在 cnt 为0的时候,一直更新 start,这样可以跳过开头连续0,当 cnt=k+1 时,更新 mid 为i,因为这是第二段中第一个1的位置,当 cnt=2*k+1 时,更新 end 为i,因为这是第三段中第一个1的位置,然后此时验证三个区间,即 A[start],A[mid],和 A[end] 必须相等,然后三个指针同时向后移动一位,若有任何不相等,就 break 掉,最后看若 end 等于n,说明三段区间中1的位置都相等,是符合题意的,返回 {start-1, mid},否则返回 {-1,-1} 即可,参见代码如下:


解法二:

class Solution {
public:
vector<int> threeEqualParts(vector<int>& A) {
int cntOne = 0, n = A.size();
for (int num : A) {
if (num == 1) ++cntOne;
}
if (cntOne == 0) return {0, n - 1};
if (cntOne % 3 != 0) return {-1, -1};
int k = cntOne / 3, start = 0, mid = 0, end = 0, cnt = 0;
for (int i = 0; i < n; ++i) {
if (A[i] == 0) continue;
if (cnt == 0) start = i;
++cnt;
if (cnt == k + 1) mid = i;
if (cnt == 2 * k + 1) {end = i; break;}
}
while (end < n && A[start] == A[mid] && A[mid] == A[end]) {
++start; ++mid; ++end;
}
if (end == n) return {start - 1, mid};
return {-1, -1};
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/927

参考资料:

https://leetcode.com/problems/three-equal-parts/

https://leetcode.com/problems/three-equal-parts/discuss/183922/C%2B%2B-O(n)-time-O(1)-space-12-ms-with-explanation-and-comments

https://leetcode.com/problems/three-equal-parts/discuss/223886/Java-O(n)-simple-solution-(don't-know-why-official-solution-is-that-long)

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 927. Three Equal Parts 三个相等的部分的更多相关文章

  1. 【leetcode】927. Three Equal Parts

    题目如下: Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these ...

  2. 927. Three Equal Parts

    Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these parts ...

  3. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  4. [Swift]LeetCode927. 三等分 | Three Equal Parts

    Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these parts ...

  5. [LintCode/LeetCode]——两数和、三数和、四数和

    LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号: ...

  6. LeetCode 259. 3Sum Smaller (三数之和较小值) $

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  7. [LeetCode] 923. 3Sum With Multiplicity 三数之和的多种情况

    Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i &l ...

  8. [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

随机推荐

  1. webpack的配置文件[webpack.config.js]

    如果项目里没有webpack.config.js这个文件,webpack会使用它本身内置在源码里的配置项. webpack.config.js这个配置名称可以通过指令修改 npx webpack -- ...

  2. 轉:StackOverflow2016最新架构探秘

    轉載:http://www.infoq.com/cn/news/2016/03/Stack-Overflow-architecture-insi?utm_source=tuicool&utm_ ...

  3. Manacher 算法学习笔记

    算法用处: 解决最长回文子串的问题(朴素型). 算法复杂度 我们不妨先看看其他暴力解法的复杂度: \(O(n^3)\) 枚举子串的左右边界,然后再暴力判断是否回文,对答案取 \(max\) . \(O ...

  4. Java日期时间API系列12-----Jdk8中java.time包中的新的日期时间API类,日期格式化,常用日期格式大全

    通过Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter, 可以看出java8的DateTimeFormatter完美解决 ...

  5. 题解 P4949 【最短距离】

    吼题啊 刚开始看上去又以为是LCT啥子的. 后来发现,TM是个图. 然后果断准备放弃,突然发现只有N个点N条边. woc,这不就一个基环树上树链剖分吗... 关于基环树问题,相信大家都一定很有经验了吧 ...

  6. Numpy中 arange() 的用法

    1. 概述Numpy 中 arange() 主要是用于生成数组,具体用法如下: 2. arange()2.1 语法numpy.arange(start, stop, step, dtype = Non ...

  7. day14-Python运维开发基础(内置函数、pickle序列化模块、math数学模块)

    1. 内置函数 # ### 内置函数 # abs 绝对值函数 res = abs(-10) print(res) # round 四舍五入 (n.5 n为偶数则舍去 n.5 n为奇数,则进一!) 奇进 ...

  8. Linux centosVMware shell 管道符和作业控制、shell变量、环境变量配置文件

    一.管道符和作业控制 管道符|,用于将前一个指令的输出作为后一个指令的输入 #cat /etc/passwd|wc -l  作业控制:当运行程序时,可以使它暂停(Ctrl+Z组合键),然后使用fg(f ...

  9. Unity 公告板 Billboard

    创建脚本如下 Billboard.cs using UnityEngine; using System.Collections; public class Billboard : MonoBehavi ...

  10. Windows7 wampServer3.0.6 Mutillidae2.7.12

    在Mac上访问虚拟机中的mutillidae,报403: By default, Mutillidae only allow access from localhost ***: Parallels ...