Given an array A of 0s and 1s, 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
class Solution {
public:
vector<int> threeEqualParts(vector<int>& A) {
int size = A.size();
int countOfOne = 0;
for (auto c : A)
if (c == 1)
countOfOne++; // if there don't have 1 in the vector
if (countOfOne == 0)
return {0, size-1}; // if the count of one is not a multiple of 3, then we can never find a possible partition since
//there will be at least one partion that will have difference number of one hence different binary
//representation
//For example, given:
//0000110 110 110
// | | |
// i j
//Total number of ones = 6
if (countOfOne%3 != 0)
return {-1, -1};
int k = countOfOne / 3;
int i;
for (i = 0; i < size; ++i)
if (A[i] == 1)
break;
int begin = i;
int temp = 0;
for (i = 0; i < size; ++i) {
if (A[i] == 1)
temp++;
if (temp == k + 1)
break;
}
int mid = i;
temp = 0;
for (i = 0; i < size; ++i) {
if (A[i] == 1)
temp++;
if (temp == 2*k+1)
break;
}
int end = i;
while (end < size && A[begin] == A[mid] && A[mid] == A[end]) {
begin++, mid++, end++;
}
if (end == size)
return {begin-1, mid};
else
return {-1, -1};
}
};

  

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 parts ...

  2. 【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 ...

  3. [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 ...

  4. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  5. [LeetCode] Split Linked List in Parts 拆分链表成部分

    Given a (singly) linked list with head node root, write a function to split the linked list into k c ...

  6. [Swift]LeetCode725. 分隔链表 | Split Linked List in Parts

    Given a (singly) linked list with head node root, write a function to split the linked list into k c ...

  7. 725. Split Linked List in Parts把链表分成长度不超过1的若干部分

    [抄题]: Given a (singly) linked list with head node root, write a function to split the linked list in ...

  8. #Leetcode# 725. Split Linked List in Parts

    https://leetcode.com/problems/split-linked-list-in-parts/ Given a (singly) linked list with head nod ...

  9. 【Leetcode】725. Split Linked List in Parts

    Given a (singly) linked list with head node root, write a function to split the linked list into k c ...

随机推荐

  1. golang的array/slice

    相同点 由相同类型的元素组合构成 元素有序排列,0为第一个元素下标 基本使用方法相同 区别 array声明时需要指定容量大小,而且无法修改 slice可通过append增加元素,当容量不够时,会自动扩 ...

  2. 代码质量检测-Sonar

    一. Sonar简介 sonarqube系统是一个代码质量检测工具 由以下四个组件组成(https://docs.sonarqube.org/display/SONAR/Architecture+an ...

  3. 配置atom需要安装的插件

    本人是一个node后端开发,这里之记录我需要的atom插件,如果需要别的请去别处查找,也省这我自己再去找 1.atom-beautify     代码格式一键美化 2.minimap  代码小地图 3 ...

  4. SQL SERVER性能优化综述

    SQL SERVER性能优化综述 一个系统的性能的提高,不单单是试运行或者维护阶段的性能调优的任务,也不单单是开发阶段的事情,而是在整个软件生命周期都需要注意,进行有效工作才能达到的.所以我希望按照软 ...

  5. MYSQl修改临时文件目录

    MSYQL在执行查询语句时报出以下错误: ERROR 3(HY000):Error writing file 'tmp/MY1yjZEI'(Errcode:28) 看了下/tmp所在目录的磁盘情况,发 ...

  6. Linux网络配置之虚拟网卡的配置(Red Hat 6.5)

    怎么查看当前的网络状态,这里就不着重描述,用ifconfig命令就可以.我们直接进入主题. red hat系统中的网卡配置在这个目录中:/etc/sysconfig/network-scripts 1 ...

  7. cJSON结构体构建

    cJSON结构体构建 一:cJSON的构建. int create_objects() { cJSON *root, *fmt, *img, *thm, *fld; char *out; int i; ...

  8. Spring 注解 整理

    首先 在xml中配置 xmlns:context="http://www.springframework.org/schema/context" http://www.spring ...

  9. 实践作业4:Web测试实践(小组作业)每日任务记录1

    会议时间:2017年12月21日会议地点:东九教学楼自习区主 持 人:王晨懿参会人员:王晨懿.余晨晨.郑锦波.杨潇.侯欢.汪元记 录 人:王晨懿会议议题:小组作业熟悉和任务分配 (一)选择待测产品 我 ...

  10. Spring框架总结(十一)

    切入点表达式 可以对指定的“方法”进行拦截:从而给指定的方法所在的类生层代理对象. 其他跟十一样,只更改bean.xml <?xml version="1.0" encodi ...