当前位置:网站首页>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};
}
}边栏推荐
- LeetCode 1200.最小绝对差
- 【Rust 笔记】16-输入与输出(下)
- 常见的最优化方法
- 【Rust 笔记】15-字符串与文本(下)
- 【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
- 884. Uncommon words in two sentences
- js快速将json数据转换为url参数
- Scope of inline symbol
- [jailhouse article] jailhouse hypervisor
- Dynamic planning solution ideas and summary (30000 words)
猜你喜欢

R语言【数据集的导入导出】

EOJ 2021.10 E. XOR tree

AtCoder Grand Contest 013 E - Placing Squares

Scope of inline symbol

How to adjust bugs in general projects ----- take you through the whole process by hand

Spark中groupByKey() 和 reduceByKey() 和combineByKey()

Wazuh開源主機安全解决方案的簡介與使用體驗

CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five

Sword finger offer 09 Implementing queues with two stacks

Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
随机推荐
AtCoder Grand Contest 013 E - Placing Squares
【Rust 笔记】14-集合(上)
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Sword finger offer 06 Print linked list from beginning to end
Simple knapsack, queue and stack with deque
多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
Implement an iterative stack
【Rust 笔记】16-输入与输出(上)
leetcode-22:括号生成
Control Unit 控制部件
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
【Rust 笔记】15-字符串与文本(上)
Some common problems in the assessment of network engineers: WLAN, BGP, switch
Scope of inline symbol
Detailed explanation of expression (csp-j 2021 expr) topic
【Rust 笔记】16-输入与输出(下)
Wazuh開源主機安全解决方案的簡介與使用體驗
“磐云杯”中职网络安全技能大赛A模块新题
Annotation and reflection
[jailhouse article] performance measurements for hypervisors on embedded ARM processors