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 <= 30000
A[i] == 0
orA[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的更多相关文章
- [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 ...
- 【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 ...
- [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 ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- [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 ...
- [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 ...
- 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 ...
- #Leetcode# 725. Split Linked List in Parts
https://leetcode.com/problems/split-linked-list-in-parts/ Given a (singly) linked list with head nod ...
- 【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 ...
随机推荐
- java使用POI写Excel文件
参考地址:http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html 1 jar包 网上下载 2 源代码 package zjr. ...
- Linux环境下搭建python+selenium+webdriver环境
1.下载并安装python,一般安装linux系统,自带有python,则python不用安装.要下载可以在官网上下载: 或者使用下面命令安装: sudo apt-get install python ...
- 用jQuery获取table中行id和td值
<%@ page language="java" pageEncoding="UTF-8"%> <% String path = reques ...
- node模块示例
来源于慕课网课程:http://www.imooc.com/video/6701 (视频) 模块的流程图如下: 做一个学校的模块示例 建一个学生的js studet.js function add(s ...
- 在SharePoint解决方案中使用JavaScript (0)
随着Web前段技术(JavaScript/HTML5)的日益发扬光大,在Web应用程序中,我们开始更多的使用JavaScript.很多以往是放在服务器上运行的逻辑,现在都开始逐渐的向前段转移.这种趋势 ...
- rocketmq--push消费过程
Rocketmq消费分为push和pull两种方式,push为被动消费类型,pull为主动消费类型,push方式最终还是会从broker中pull消息.不同于pull的是,push首先要注册消费监听器 ...
- 微信小程序(应用号)开发教程
本文档将带你一步步创建完成一个微信小程序,并可以在手机上体验该小程序的实际效果.这个小程序的首页将会显示欢迎语以及当前用户的微信头像,点击头像,可以在新开的页面中查看当前小程序的启动日志.下载源码 1 ...
- 26-三个水杯(bfs)
三个水杯 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有 ...
- linux操作系统下,怎么使用kill按照PID一次杀死多个进程
1.ps -ef | grep firefox | grep -v grep | cut -c 9-15 | xargs kill -s 9 说明:“grep firefox”的输出结果是,所有含有关 ...
- Openssl oscp命令
一.简介 ocsp,在线证书状态命,能够执行很多OCSP的任务,可以被用于打印请求文件和响应文件, 二.语法 openssl ocsp [-out file] [-issuer file] [-cer ...