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. .net core HttpClient 使用之掉坑解析(一)

    一.前言 在我们开发当中经常需要向特定URL地址发送Http请求操作,在.net core 中对httpClient使用不当会造成灾难性的问题,这篇文章主要来分享.net core中通过IHttpCl ...

  2. 广告行业中那些趣事系列10:推荐系统中不得不说的DSSM双塔模型

    摘要:本篇主要介绍了项目中用于商业兴趣建模的DSSM双塔模型.作为推荐领域中大火的双塔模型,因为效果不错并且对工业界十分友好,所以被各大厂广泛应用于推荐系统中.通过构建user和item两个独立的子网 ...

  3. flink源码阅读

    Flink面试--源码篇 1.Flink Job的提交流程? 2.Flink所谓"三层图"结构是哪几个"图"? 3.JobManger在集群中扮演了什么角色? ...

  4. springmvc 校验---spring校验

    springmvc提供了灵活的可拓展的校验方式,根据不同的项目可选择适合的校验方式,首先介绍下springmvc中内置的校验实现方式! 1.实现 org.springframework.validat ...

  5. react 动态渲染echarts折线图,鼠标放大缩小

    //折线图组件import React,{Component} from 'react'; import ReactEcharts from 'echarts-for-react'; class Ec ...

  6. hadoop与spark的处理技巧(一)Top N处理技巧

    1.MR的topN处理方案,假设所有输入Key都唯一 2.MR的topN处理方案,假设输入Key不唯一 3.spark的topN处理方案,假设所有输入Key都唯一,不使用top()和takeOrder ...

  7. 10.2 Go redis

    10.2 Go redis redis是NoSQL数据, 不是传统的关系型数据库.linux,windows环境皆可安装. https://redis.io http://www.redis.cn r ...

  8. strut2登陆注册验证码

    1. 生成图片和验证码 package com.jmu.code; import java.awt.Color; import java.awt.Font; import java.awt.Graph ...

  9. 从无到有Springboot整合Spring-data-jpa实现简单应用

    本文介绍Springboot整合Spring-data-jpa实现简单应用 Spring-data-jpa是什么?这不由得我们思考一番,其实通俗来说Spring-data-jpa默认使用hiberna ...

  10. bzoj 1072状压DP

    1072: [SCOI2007]排列perm Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2293  Solved: 1448[Submit][St ...