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. 浅谈JobExecutionContext&JobDataMap

  2. java aop 日志打印 正则设置

    package tz.lion.Utils.aop; import com.alibaba.fastjson.JSON;import org.springframework.web.multipart ...

  3. 混合开发Js bridge新秀-DSBridge iOS篇

    这个DSBridge 和我之前开发做的混合开发 用的方式 很相似,所以觉得很是不错,推荐给你大家. DSBridge-IOS:https://github.com/wendux/DSBridge-IO ...

  4. UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释(转)

    layoutSubviews总结 ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size- (void)sizeToFit—————— - (void ...

  5. 解决git无法clone地址为https的库

    一.问题描述 早上在学习<Spark快速大数据分析>的时候,需要下载书本的实例代码,于是用git clone一下给出的库: https://github.com/databricks/le ...

  6. C#中读写配置参数文件(利用Windows的API)

      读配置文件与写配置文件的核心代码如下:   [DllImport("kernel32")] // 读配置文件方法的6个参数:所在的分区(section).键值. 初始缺省值. ...

  7. Android中Cursor类的概念和用法[转]

    首页 > 程序开发 > 移动开发 > Android > 正文   Android中Cursor类的概念和用法 2011-09-07      0个评论       收藏    ...

  8. [C++] c pointer

    the nature of pointer const keyword const int*  p int const *p int*  const p int const a const int a ...

  9. asp.net web api 2框架揭秘文摘

    第一章 概述 URI 统一资源标识符 URL 统一资源定位符 http方法:get,post,put,delete,head等 状态码:100-199,请求已被接受: 200-299,成功状态: 30 ...

  10. Maven及POM文件

    Maven Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. Logback是由LOG4创始人设计的又一个开源日志组件. 相关链接: Ma ...