当前位置:网站首页>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};
}
}边栏推荐
猜你喜欢

Implement an iterative stack

QQ电脑版取消转义符输入表情

Personal developed penetration testing tool Satania v1.2 update

1.14 - 流水线

Implement a fixed capacity stack

可变电阻器概述——结构、工作和不同应用

Solution to the palindrome string (Luogu p5041 haoi2009)

Full Permutation Code (recursive writing)

Sword finger offer 53 - ii Missing numbers from 0 to n-1

Sword finger offer 58 - ii Rotate string left
随机推荐
1.15 - 输入输出系统
【Rust 笔记】13-迭代器(下)
Scope of inline symbol
Simply sort out the types of sockets
过拟合与正则化
Convolution neural network -- convolution layer
[jailhouse article] jailhouse hypervisor
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
智慧工地“水电能耗在线监测系统”
R语言【数据集的导入导出】
卷积神经网络——卷积层
【Rust 笔记】17-并发(上)
Time complexity and space complexity
【实战技能】非技术背景经理的技术管理
Daily question 1688 Number of matches in the competition
从Dijkstra的图灵奖演讲论科技创业者特点
ALU逻辑运算单元
Dynamic planning solution ideas and summary (30000 words)
LVS简介【暂未完成(半成品)】
Smart construction site "hydropower energy consumption online monitoring system"