当前位置:网站首页>[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;
}
边栏推荐
- Stm32f1 and stm32subeide programming example - device driver -dht11 temperature sensor driver
- Pre research of image scanning tool
- Successfully rated Tencent t3-2, 10000 word parsing
- Pytorch官方Faster R-CNN源代码解析(一)——特征提取
- 移动应用出海的“新大陆”
- Openstack network
- 嵌入式系统硬件构成-基于ARM的嵌入式开发板介绍
- 看完这一篇就够了,web中文开发
- 手把手教你IDEA创建SSM项目结构
- 【刷题篇】超级洗衣机
猜你喜欢
![[wechat applet development] Part 1: development tool installation and program configuration](/img/a8/f4dcbde295ba7cf738d878464b3af0.png)
[wechat applet development] Part 1: development tool installation and program configuration

Unittest framework

嵌入式系统概述1-嵌入式系统定义、特点和发展历程

嵌入式系统概述3-嵌入式系统的开发流程和学习基础、方法

成功跳槽阿里,进阶学习

Build an embedded system software development environment - build a cross compilation environment

Volume mount and mirror creation

中科物栖CEO张磊:“芯片+OS”范式在万物互联时代的机遇与挑战|量子位·视点分享回顾...

嵌入式系統硬件構成-基於ARM的嵌入式開發板介紹

嵌入式系统概述2-嵌入式系统组成和应用
随机推荐
Actual combat | realizing monocular camera ranging by skillfully using pose solution
Five ways to quickly download large files from Google cloud disk
A brief introduction to Verilog mode
Successfully rated Tencent t3-2, 10000 word parsing
[cloud native | kubernetes] in depth understanding of deployment (VIII)
Pytoch official fast r-cnn source code analysis (I) -- feature extraction
itk neighbhood
关于#SQLite写注册功能时,数据表查询出错#的问题,如何解决?
Online picture material
Getting to know blob objects
Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver
import torch_geometric 加载一些常见数据集
About paiwen
【刷题篇】抽牌获胜的概率
Experience and learning path of introductory deep learning and machine learning
Overview of embedded system 1- definition, characteristics and development history of embedded system
Openmax (OMX) framework
在 Debian 10 上独立安装MySQL数据库
镜像扫描工具预研
How to adapt the page size when iframe is embedded in a web page