当前位置:网站首页>927. Trisection simulation
927. Trisection simulation
2022-07-05 06:02:00 【Empress Yu】
927. Three equal parts
Given a by
0and1Array of componentsarr, Divide the array into 3 A non empty part , Make all these parts represent the same binary value .If it can be done , Please return whatever
[i, j], amongi+1 < j, thus :
arr[0], arr[1], ..., arr[i]For the first part ;arr[i + 1], arr[i + 2], ..., arr[j - 1]For the second part ;arr[j], arr[j + 1], ..., arr[arr.length - 1]For the third part .- The binary values represented by these three parts are equal .
If you can't do it , Just go back to
[-1, -1].Be careful , When considering the binary represented by each part , It should be seen as a whole . for example ,
[1,1,0]Represents... In decimal system6, Not really.3. Besides , Leading zero is also Allowed Of , therefore[0,1,1]and[1,1]Represents the same value .Example 1:
Input :arr = [1,0,1,0,1] Output :[0,3]Example 2:
Input :arr = [1,1,0,1,1] Output :[-1,-1]Example 3:
Input :arr = [1,1,0,0,1] Output :[0,2]Tips :
3 <= arr.length <= 3 * 10^4arr[i]yes0or1
source : Power button (LeetCode)
link :https://leetcode.cn/problems/three-equal-parts
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The result of doing the question
success , This problem requires a clear simulation idea
Method : simulation
1. 0 Because there is a leading 0, So I'm not sure , however 1 It must be able to divide into 3 Share , Here we can judge . Special all 0 return ,[0,n-1]
2. The tail 0 The number of is from the last number to the last 1 after 0 Determined by the number of , these 0 Not a leader 0 Can't erase
3. With the tail 0 The number of , With 1 The number of , You can separate 3 Share , Finally, compare whether the three synthesized copies are all equal
class Solution {
public int[] threeEqualParts(int[] arr) {
int cnt = 0;
for(int v:arr){
cnt += v;
}
int n = arr.length;
if(cnt == 0) return new int[]{0,n-1};
if(cnt%3!=0) return new int[]{-1,-1};
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder();
int zeroCnt = 0;
for(int i = n-1; i>=0;i--){
if(arr[i]==1){
zeroCnt = n-i-1;
break;
}
}
int curr = 0;
int endZero = 0;
int[] ans = new int[]{-1,-1};
for(int i = 0; i < n; i++){
if(curr <cnt/3||endZero<zeroCnt){
if(arr[i]==1){
sb1.append("1");
++curr;
endZero = 0;
}
else if(sb1.length()>0) {
sb1.append("0");
endZero++;
}
}else if(curr <cnt/3*2||endZero<zeroCnt*2){
if(ans[0]==-1) ans[0] = i-1;
if(arr[i]==1){
sb2.append("1");
++curr;
endZero = zeroCnt;
}
else if(sb2.length()>0){
sb2.append("0");
endZero++;
}
}else{
if(ans[1]==-1) ans[1] = i;
if(arr[i]==1){
sb3.append("1");
++curr;
}
else if(sb3.length()>0) sb3.append("0");
}
}
return sb1.toString().equals(sb2.toString()) && sb2.toString().equals(sb3.toString())?ans:new int[]{-1,-1};
}
}边栏推荐
- 【Rust 笔记】16-输入与输出(下)
- How to adjust bugs in general projects ----- take you through the whole process by hand
- LeetCode 1200.最小绝对差
- 1039 Course List for Student
- 全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
- 剑指 Offer II 058:日程表
- 【Rust 笔记】14-集合(下)
- 2022 极术通讯-Arm 虚拟硬件加速物联网软件开发
- EOJ 2021.10 E. XOR tree
- Dichotomy, discretization, etc
猜你喜欢
![[article de jailhouse] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[article de jailhouse] jailhouse hypervisor

Fried chicken nuggets and fifa22

On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech

【Jailhouse 文章】Jailhouse Hypervisor

7. Processing the input of multidimensional features

Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module

CF1634 F. Fibonacci Additions

Wazuh开源主机安全解决方案的简介与使用体验
![[jailhouse article] performance measurements for hypervisors on embedded ARM processors](/img/c0/4843f887f77b80e3b2329e12d28987.png)
[jailhouse article] performance measurements for hypervisors on embedded ARM processors

leetcode-6110:网格图中递增路径的数目
随机推荐
Daily question 2006 Number of pairs whose absolute value of difference is k
leetcode-6109:知道秘密的人数
leetcode-22:括号生成
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
1039 Course List for Student
【Jailhouse 文章】Jailhouse Hypervisor
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
CPU内核和逻辑处理器的区别
Sword finger offer 53 - ii Missing numbers from 0 to n-1
Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
How to adjust bugs in general projects ----- take you through the whole process by hand
每日一题-无重复字符的最长子串
卷积神经网络简介
Daily question 2013 Detect square
Time complexity and space complexity
打印机脱机时一种容易被忽略的原因
[jailhouse article] jailhouse hypervisor
QQ电脑版取消转义符输入表情
1.14 - 流水线
A misunderstanding about the console window