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 ...
随机推荐
- 文件 md5 查看 命令
Windows命令查看文件MD5 certutil -hashfile yourfilename.ext MD5 certutil -hashfile yourfilename.ext SHA1 ...
- linux开机自检配置文件fstab变只读无法修改问题
控制linux开机自检的配置文件是/etc/fstab,在最近用的服务器中,发现fstab变成了只读权限,无法修改. 解决方法:RH5下,因磁盘改变,而导致系统停在Ctrl+d,此时需输入密码进入修改 ...
- Scala基础:闭包、柯里化、隐式转换和隐式参数
闭包,和js中的闭包一样,返回值依赖于声明在函数外部的一个或多个变量,那么这个函数就是闭包函数. val i: Int = 20 //函数func的方法体中使用了在func外部定义的变量 那func就 ...
- 80. Remove Duplicates from Sorted Array II (Array)
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- 40 Questions to test your skill in Python for Data Science
Comes from: https://www.analyticsvidhya.com/blog/2017/05/questions-python-for-data-science/ Python i ...
- Fix: The account is not authorized to log in from this station
If you have more the one computers running Windows, then its possible to connect them using HomeGrou ...
- ECS 游戏架构 理解
转载自:http://blog.csdn.net/i_dovelemon/article/details/25798677 理解 组件-实体-系统 (ECS \CES)游戏编程模型 - 博客频道 ...
- Spring.net 事件的注入
1.首先上客户端代码 static void Main(string[] args) { IApplicationContext ctx = ContextRegi ...
- Linux Mint 17 搭建 JSP 环境
一.配置Tomcat 服务器 1.下载 tomcat 2.解压后放到/usr/local目录下面 3.以root权限执行 chmod +x *.sh 4.启动 ./startup.sh#方式1 ./ ...
- javascript总结36:DOM-点击按钮切换图片案例
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...