当前位置:网站首页>[Title brushing] Super washing machine
[Title brushing] Super washing machine
2022-06-12 13:10:00 【m0_ sixty million six hundred and thirty-one thousand three hun】
One 、 subject
Suppose there is n The super washing machine is on the same row . At the beginning , There may be a certain amount of clothes in each washing machine , Or it could be empty .
In each step , You can choose whatever m (1 <= m <= n) Washing machine , At the same time, send a piece of clothes from each washing machine to an adjacent washing machine .
Given an array of integers machines Represents the number of clothes in each washing machine from left to right , Please give the number of clothes left in all washing machines equal Minimum number of operation steps . If you can't make the number of clothes in each washing machine equal , Then return to -1 .

Two 、 Answer key
2.1 Ideas
Calculate the answer at a single point and then decide the final result

2.2 Source code
public static int findMinMoves(int[] machines) {
if(machines==null||machines.length==0){
return 0;
}
int size=machines.length;
int sum=0;
for (int i = 0; i < size; i++) {
sum+=machines[i];
}
if(sum%size!=0){
return -1;
}
int avg=sum/size;
int leftSum=0;
int ans=0;
for (int i = 0; i < machines.length; i++) {
int leftRest=leftSum-i*avg;
int rightRest=(sum-leftSum-machines[i])-(size-i-1)*avg;
if(leftRest<0&&rightRest<0){
ans=Math.max(ans,Math.abs(leftRest)+Math.abs(rightRest));
}else {
ans=Math.max(ans,Math.max(Math.abs(leftRest),Math.abs(rightRest)));
}
leftSum+=machines[i];
}
return ans;
}
边栏推荐
- 5V升压到12.6V的锂电池充电IC芯片方案FS4062B
- 嵌入式系统概述1-嵌入式系统定义、特点和发展历程
- 成功定级腾讯T3-2,万字解析
- Overview of embedded system 1- definition, characteristics and development history of embedded system
- When to use binary search
- Source of routing information
- Stm32f1 and stm32subeide programming example - device driver -dht11 temperature sensor driver
- leetcode 47. Permutations II full permutations II (medium)
- 移动应用出海的“新大陆”
- R language ggplot2 visualization: use the ggrep package to add a number label to the data point at the end of the line plot
猜你喜欢

下一个职场演讲PPT的明星,会不会是此刻的你【完美总结】

Eight misunderstandings are broken one by one (2): poor performance? Fewer applications? You worry a lot about the cloud!

Binary tree (serialization)

leetcode 47. Permutations II full permutations II (medium)

多源BFS问题 模板(附题)

Wechat web developer tools tutorial, web development issues

"New continent" of mobile application going to sea

干货满满,这些知识你必须拿下

torch_geometric message passing network

Actual combat | realizing monocular camera ranging by skillfully using pose solution
随机推荐
Actual combat | realizing monocular camera ranging by skillfully using pose solution
软件构造 03 正则表达式
leetcode 47. Permutations II full permutations II (medium)
Three dimensional coordinate point fitting sphere (MATLAB and C)
[you code, I fix] whitesource was officially renamed mend
Five ways to quickly download large files from Google cloud disk
干货满满,这些知识你必须拿下
How to balance multiple losses in deep learning?
Jacobian matrix IK of manipulator
itk neighbhood
Dameng database DM8 Windows environment installation
Further understanding of the network
itk::SymmetricForcesDemonsRegistrationFilter
Chrome debugging tool
Mui login database improvement and Ajax asynchronous processing [mui+flask+mongodb+hbuilderx]
微信web开发者工具使用教程,web开发问题
ITK Examples/RegistrationITKv4/DeformableRegistration
IC chip scheme fs4062b for lithium battery charging with 5V boost to 12.6V
嵌入式系统概述3-嵌入式系统的开发流程和学习基础、方法
号称下一代监控系统!来看看它有多牛逼