当前位置:网站首页>517. Super washing machine
517. Super washing machine
2022-07-26 05:21:00 【Xiao Lu wants to brush the questions】
List of articles
Preface
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 .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/super-washing-machines
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
One 、 Their thinking
Ideological tradition : The bottleneck of single point , Finally, look at the relationship between the total answer and the single point bottleneck
Suppose you come to a certain station (i Number ) Washing machine Number of clothes ?
Suppose we know the average of each machine 
The left side is under 15 Pieces of . And there are many on the right side 10 Pieces of , Suppose there are always clothes to move , At least a few rounds .
At the very least 15 round 
i If there are very few clothes, you may have to give it clothes on both sides 
At least it needs 15 round 
If the left side owes 15 Pieces of . On the right side 7 Pieces of , I ask how many rounds you want to move , Both sides are counting on i output , It can only throw one at a time .
Therefore need 22 round
First calculate the total number of clothes , You can calculate the cumulative sum of the left part , i The position has its own value .
There are still a few more pieces on the left side , Is the right part short or extra . Can be calculated
There is a total number of clothes , There is one i The cumulative sum on the left , Next you go to any one i Location .
Your left side , Is the right side more or less ? You can figure it out 
According to our strategy . We figure out how many rounds the bottleneck takes at zero ,1 How many rounds does the bottleneck need in position ,
2 How many rounds does the bottleneck need in position , Each bit We should talk more about the bottleneck of setting . The conclusion is the most painful point of all the answers max, Determines the overall bottleneck .
Because when the most painful bottleneck is satisfied , The other bottleneck synchronization is solved
Because he can move in parallel in each round . So your most painful bottleneck determines the total number of rounds . No, why is mathematical proof troublesome
Code
class Solution {
public int findMinMoves(int[] arr) {
if (arr == null || arr.length == 0) {
return 0;
}
int size=arr.length;
int sum=0;
for(int i=0;i<size;i++){
sum+=arr[i];
}
if(sum%size!=0){
return -1;
}
int avg=sum/size;
int leftSum=0;
int ans=0;
for(int i=0;i<size;i++){
// The number of differences on the left
int leftRes=leftSum-i*avg;
// The number of differences on the right
int rightRes=(sum-leftSum-arr[i])-(size-i-1)*avg;
if(leftRes<0&&rightRes<0){
ans=Math.max(ans,Math.abs(leftRes)+Math.abs(rightRes));
}else{
ans=Math.max(ans,Math.max(Math.abs(leftRes),Math.abs(rightRes)));
}
leftSum+=arr[i];
}
return ans;
}
}
边栏推荐
- NetCore MySql The user specified as a definer (‘admin‘@‘%‘) does not exist
- OD-Paper【2】:Fast R-CNN
- Seata submits at details in two stages
- 动态内存管理及柔性数组
- Chinese character style transfer --- learn the conversion and generation of one to many programmed Chinese characters through generation confrontation network
- ThreadLocal transfer between parent and child threads in asynchronous
- [acwing] 1268. Simple questions
- TZC 1283: simple sort - Comparative sort
- Leetcode linked list problem - 206. reverse linked list (learn linked list by one question and one article)
- Mysql优化
猜你喜欢

Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output

LeetCode链表问题——206.反转链表(一题一文学会链表)

ALV程序收集

OD-Paper【1】:Rich feature hierarchies for accurate object detection and semantic segmentation

代码审计之百家cms

Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0

Real scientific weight loss

IVR在voip电话系统的应用与价值
![Meta analysis [whole process, uncertainty analysis] method based on R language and meta machine learning](/img/87/9f8353c5c9c700eaa63f66697aa44a.png)
Meta analysis [whole process, uncertainty analysis] method based on R language and meta machine learning

测试必备工具之Fiddler,你真的了解吗?
随机推荐
Week 6 Learning Representation: Word Embedding (symbolic →numeric)
测试必备工具之Fiddler,你真的了解吗?
Nacos introduction and deployment
Princeton calculus reader 02 Chapter 1 -- composition of functions, odd and even functions, function images
使用Ansible中的playbook
Simulation of future air pollution changes
Chinese character style transfer --- learn the conversion and generation of one to many programmed Chinese characters through generation confrontation network
怎么办理聚合收款码
DOM操作--操作节点
安装NCCL\mpirun\horovod\nvidia-tensorflow(3090Ti)
循环结构 practice
Migrate the server and reconfigure the database (the database has no monitoring, and the monitoring starts with tns-12545, tns-12560, tns-00515 errors)
If MySQL calculates the current month change / current month increase / year-on-year change / year-on-year increase?
手把手教你用代码实现SSO单点登录
C语言函数
How to reproduce the official course of yolov5 gracefully (II) -- Mark and train your own data set
CMD operation command
Hack The Box - Web Requests Module详细讲解中文教程
测试用例评审如何开展
TZC 1283: simple sort - Comparative sort