517. 超级洗衣机

假设有 n 台超级洗衣机放在同一排上。开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的。

在每一步操作中,你可以选择任意 m (1 ≤ m ≤ n) 台洗衣机,与此同时将每台洗衣机的一件衣服送到相邻的一台洗衣机。

给定一个非负整数数组代表从左至右每台洗衣机中的衣物数量,请给出能让所有洗衣机中剩下的衣物的数量相等的最少的操作步数。如果不能使每台洗衣机中衣物的数量相等,则返回 -1。

示例 1:

输入: [1,0,5]

输出: 3

解释:

第一步:    1     0 <-- 5    =>    1     1     4
第二步: 1 <-- 1 <-- 4 => 2 1 3
第三步: 2 1 <-- 3 => 2 2 2

示例 2:

输入: [0,3,0]

输出: 2

解释:
第一步: 0 <-- 3 0 => 1 2 0
第二步: 1 2 --> 0 => 1 1 1

示例 3:

输入: [0,2,0]

输出: -1

解释:

不可能让所有三个洗衣机同时剩下相同数量的衣物。

提示:

n 的范围是 [1, 10000]。

在每台超级洗衣机中,衣物数量的范围是 [0, 1e5]。

PS:

我移动的最小的次数,无非来源于两种

我当前值和平均值的差

我前面差的和

class Solution {
public int findMinMoves(int[] machines) {
int sum = 0;
for(int num : machines)
sum += num;
if(sum % machines.length != 0)
return -1; int target = sum / machines.length;
int res = 0, balance = 0;
for(int i = 0 ; i < machines.length; i ++){
balance += machines[i] - target;
res = Math.max(res, Math.max(machines[i] - target, Math.abs(balance)));
}
return res;
}
}

Java实现 LeetCode 517 超级洗衣机的更多相关文章

  1. Leetcode 517.超级洗衣机

    超级洗衣机 假设有 n 台超级洗衣机放在同一排上.开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的. 在每一步操作中,你可以选择任意 m (1 ≤ m ≤ n) 台洗衣机,与此同时将每台洗衣机 ...

  2. Java实现 LeetCode 372 超级次方

    372. 超级次方 你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出. 示例 1: 输入: a = 2, b = [3] 输出: 8 示例 2: ...

  3. Java实现 LeetCode 313 超级丑数

    313. 超级丑数 编写一段程序来查找第 n 个超级丑数. 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数. 示例: 输入: n = 12, primes = [2,7, ...

  4. [Swift]LeetCode517. 超级洗衣机 | Super Washing Machines

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. [hdu5534]DP

    题目原意:给一棵n个点的树添加边,给定度函数f(d)为一个点的度的函数,求所有点的度函数的和 思路: 函数只与点的度有关,而与点无关,n个点的树有n-1条边,共产生2(n-1)个度,每个点至少有1个度 ...

  2. [zoj3632]线段树的应用

    题意:f[i] = min(f[i+L]~f[i+R]) + x,计算f数组.从大到小计算即可,用线段树维护一下. #pragma comment(linker, "/STACK:10240 ...

  3. 第六次java上机作业

    .编写一个简单程序,要求数组长度为5,静态赋值10,,,,,在控制台输出该数组的值. package mm; public class Test { public static void main(S ...

  4. EOS基础全家桶(十一)智能合约IDE-EOS_Studio

    简介 我们马上要进入智能合约的开发了,以太坊最初提供了智能合约的功能,并宣告区块链进入2.0时代,而EOS的智能合约更进一步,提供了更多的便利性和可能性.为了进一步了解智能合约,并进行开发,我们需要先 ...

  5. angular js 分页

    一.编写实体类PageResult public class PageResult implements Serializable { private Long total;//总记录数 privat ...

  6. java经典问题 byte b=1、b=b+1、b+=1

    直接问题: 首先 byte的范围 [-128,127] byte 类型可以自动转为int类型 int类型不能自动转为byte类型. 超过byte的范围,就会变成int类型了 byte b=1:正确, ...

  7. Vi 和 Vim 的使用

    Vi (Visual Interface)是 Linux下基于Shell 的文本编辑器,Vim (Visual Interface iMproved)是 Vi的增强版本,扩展了很多功能,比如对程序源文 ...

  8. 4.2 Go switch

    4.2 Go switch switch语句用于基于不同条件执行不同动作,每一个case分支唯一,自上而下逐一测试,直到匹配结束,默认自动终止,不需要break. 2. switch基本语法 swit ...

  9. Java基础知识面试题及答案-整理

    1.String类可以被继承吗?  不能.String类在声明中使用final关键字修饰符.使用final关键字修饰的类无法被继承. Java语言的开发者为什么要将String类定义为final类呢? ...

  10. python3.x 基础五:模块

    1.定义 模块:本质是.py结尾的python文件,从逻辑上组织python代码,可以是变量,函数,类,逻辑,目的是实现一个功能,test.py 对应模块名:test 包:从逻辑上组织模块的,本质就是 ...