[LeetCode] 927. Three Equal Parts 三个相等的部分
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, andA[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:
3 <= A.length <= 30000A[i] == 0orA[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/
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 927. Three Equal Parts 三个相等的部分的更多相关文章
- 【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 ...
- 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 ...
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- [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 ...
- [LintCode/LeetCode]——两数和、三数和、四数和
LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号: ...
- 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 < ...
- [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 ...
- [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
随机推荐
- jdbc学习over
这次的代码和之前学习到一般的代码主要就是将一些很常见的操作(建立连接.清除连接)不管做什么操作都需要用到它们,所以将它们单独放到另一个工具类里面去. 用到的术语: 1.事务:https://www.c ...
- SQL数据库入门基础
SQL(Structure Query Language,结构化查询语言)语言是国际标准化组织(ISO)采纳的标准数据库语言. 数据库就是一幢大楼,我们要先盖楼,然后再招住户(住户当然就是数据库对 ...
- PowerHandle管理页面
HljioGBjiomGiiuHJKYiubfcgydftyjkKHKHIUvghcghftyfhkHJKGHJKguyjljklhJKGUYGUYhjklhfgtydtyGHJgHJUGuy HKk ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:禁用按钮
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- PHP 三元运算符
$a = $a ? $a : 1;//第一种 $a = $a ? : 1;//第二种 第二种写法从 PHP 5.3 引入,表示若 $a 返回值为真时,取 $a 的返回值. 此外,在 PHP7 中引入了 ...
- 为Linux环境安装图形化界面
1.更新软件源并升级系统 yum uppdate&&yum install wqy-microhei-fonts 2.安装GNOME+VNC一键包 wget https://gist. ...
- Javaweb实现简单的选课系统(主要技术包括jsp、Servlet、Javabean)
一.背景信息: 首先,这个选课系统是上周一老师在课堂上让我们做的测试,考试时长三个小时,我只做了一半,也没有实现选课流程. 由于上周忙于写实验报告没有时间继续完成这个测试.这周用前天和昨天一共七个小时 ...
- RabbitMq学习笔记——RabbitMQ C的使用
1.需要用到的参数: 主机名:hostname.端口号:port.交换器:exchange.路由key:routingkey .绑定路由:bindingkey.用户名:user.密码:psw,默认用户 ...
- Unnatural
1. 纪录片:非自然选择 1.1 CRISPR-Cas9的出现 1.2 故事1:先天性基因缺陷而失明的小孩 1.3 故事2:基因变异的蚊子 1.4 基因技术应用的现状 1.5 担忧 2. CRISPR ...
- Day8 - C - Largest Rectangle in a Histogram HDU - 1506
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...