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. quartz简介

  2. 【codevs3160】 LCS 【后缀自动机】

    题意 给出两个字符串,求它们的最长公共子串. 分析 后缀自动机的基础应用. 比如两个字符串s1和s2,我们把s1建为SAM,然后根据s2跑,找出s2每个前缀的最长公共后缀. 我们可以理解为,当向尾部增 ...

  3. 69. Sqrt(x) (Divide-and-Conquer)

    Implement int sqrt(int x). Compute and return the square root of x. 注意: 计算平方的时候可能会溢出,所以mid要定义为long 另 ...

  4. telnet客户端模拟浏览器发送请求

    telnet 客户端 telnet客户端能够发出请求去连接服务器(模拟浏览器) 使用telnet之前,需要开启telnet客户端 1.进入控制面板 2.进入程序和功能,选择打开或关闭windows功能 ...

  5. [SoapUI] SoapUI可以做到些什么?功能有多强大?

    SoapUI. The Swiss-Army Knife of Testing. Whether you’re a tester, developer, business analyst, or ma ...

  6. SSL与TLS有什么区别

    SSL与TLS有什么区别(最全面的知识点都在这) 发布日期:2018-10-12SSL:(Secure Socket Layer,安全套接字层),位于可靠的面向连接的网络层协议和应用层协议之间的一种协 ...

  7. HUST软测1504班第4周小组作业成绩:WordCount优化

    说明 本次公布的成绩为第四周作业的结果: 第4周小组作业:WordCount优化 博客推荐:本次作业有一位同学完成有创意,推荐优秀博客.(优秀博客不会对成绩带来正面或者负面影响)PS:做任何创新的任务 ...

  8. python 中面向对象编程简单总结3--定制类

    声明:资源来自慕课网python学习课程,以下只是个人学习总结,仅供参考 1.Python类的特殊方法 特征:以  __ 开头并结尾的方法,比如用于print的__str__() , __getatt ...

  9. Spring下集成ActiveMQ推送

    本文是将ActiveMQ消息制造者集成进spring,通过spring后台推送消息的实现. 首先是spring的applicationContext的配置,如下 <?xml version=&q ...

  10. 3.1.7 线程阻塞工具类:LockSupport

    package 第三章.线程阻塞工具LockSupport; import java.util.concurrent.locks.LockSupport; /** * Created by zzq o ...