▶ 超级洗碗机。给定一个有 n 元素的整数数组,我们把 “将指定位置上元素的值减 1,同时其左侧或者右侧相邻元素的值加 1” 称为一次操作,每个回合内,可以选定任意 1 至 n 个位置进行独立的操作,求最少的回合数,使得该数组中的搜有元素调整为相等的值。若不存在(所有元素的和不能被元素个数整除),返回 -1 。

● 代码,14 ms,最大差距法。考虑将所有元素的平均值平移到 0,记此时 machines[ 0 ] == a,如果 a == 0,则 machines[ 0 ]已经调整完成,跳过;若 a > 0,则考虑对 machines[ 0 ] 做 a 次操作将多余部分转移到 machines[ 1 ] 上;若 a < 0,则考虑对 machines[ 1 ] 做 a 次操作,补充 machines[ 0 ] 缺少的部分。然后对 machines[1] 做相同的分析,以此类推向后处理。发现在这一过程中,所需的最大回合数实际上取决于两个数:①某个元素本身的绝对值大小(如所有元素都是0,只有一个元素为 10000,则回合数取决于该值);②从 machines[ 0 ] 开始积累到该元素为止的(比如前三个元素为分别为 1,2,3,则分析 machines[3] 时前面已经积累了 6 个单位,至少需要 6 回合才能消灭这个峰)。

 class Solution
{
public:
int findMinMoves(vector<int>& machines)
{
const int n = machines.size();
int i, total, average, count, maxnum;
for (i = total = ; i < n; total += machines[i++]);
if (total % n)
return -;
for (average = total / n, i = count = maxnum = ; i < n; i++)
{
count += machines[i] - average;// 计算当前格点和平均值的差距
maxnum = max(max(maxnum, abs(count)), machines[i] - average);// 比较历史maxnum,累计差距,当前差距中最大者
}
return maxnum;
}
};

● 逐格分析,14 ms

■ 先计算数组 machiunes 的累积和 sum,可以用其最后一个元素来计算平均值以及判断是否有解

■ 对于第 i 个格点,计算其左侧编号为 0 ~ i-1 的 i 个格点的 数值需求量(i * avg) - 数值保有量(sum[i]),记作 lp

■ 其右侧编号为 i+1 ~ machines.size()-1 的 n-1-i 个格点类似处理,结果记作 rp

■ 若 lp > 0 && rp > 0,说明两边都缺货,应该从第 i 格点往两边分配,需要 abs(lp) + abs(rp) 回合

■ 若 lp < 0 && rp < 0,说明两边都有富余,应该从两边向第 i 格点分配,需要 max(abs(lp), abs(rp)) 回合

■ 若 lp < 0 && rp > 0 || lp > 0 && rp < 0,说明两侧不均等,需要借助第 i 格点进行传递,需要 max(abs(L), abs(rp)) 回合

 class Solution
{
public:
int findMinMoves(vector<int>& machines)
{
const int len = machines.size();
int i, avg, res, lp, rp;
vector<int> sum(len + , );
for (i = ; i < len; i++)// 累积和
sum[i + ] = sum[i] + machines[i];
if (sum[len] % len)
return -;
for (avg = sum[len] / len, i = res = ; i < len; i++)
{
lp = i * avg - sum[i];
rp = (len - i - ) * avg - (sum[len] - sum[i] - machines[i]);
if (lp > && rp > )
res = max(res, abs(lp) + abs(rp));
else
res = max(res, max(abs(lp), abs(rp)));
}
return res;
}
};

517. Super Washing Machines的更多相关文章

  1. Leetcode - 517 Super Washing Machines

    今天开始定期记录本人在leetcode上刷题时遇到的有意思的题目.   517. Super Washing Machines   You have n super washing machines ...

  2. 517 Super Washing Machines 超级洗衣机

    详见:https://leetcode.com/problems/super-washing-machines/description/ C++: class Solution { public: i ...

  3. 第十五周 Leetcode 517. Super Washing Machines(HARD) 贪心

    Leetcode517 很有趣的一道题 由于每一步可以任选某些数字对它们进行转移,所以实际上是在求最优解中的最复杂转移数. 那么我们考虑,到底哪一个位置要经过的流量最大呢? 枚举每个位置,考虑它左边的 ...

  4. [LeetCode] Super Washing Machines 超级洗衣机

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

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

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

  6. LeetCode517. Super Washing Machines

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

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. LeetCode Weekly Contest 20

    1. 520. Detect Capital 题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断.还有:我感觉自己写的代码很丑,判断条件比较多,需要改进 ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. 【原创】遇到:Invalid layout of java.lang.String at value 这样的问题,该怎么办呢?

    Invalid layout of java.lang.String at value## A fatal error has been detected by the Java Runtime En ...

  2. css实现心形图案

    用1个标签实现心形图案,show you the code; <!DOCTYPE html> <html lang="en"> <head> & ...

  3. poj3422

    题解: 先奖每一个点裂开来 然后在见图 代码: #include<cstdio> #include<cmath> #include<cstring> #includ ...

  4. CMake与Make

    大家都知道,写程序大体步骤为: 1.用编辑器编写源代码,如.c文件. 2.用编译器编译代码生成目标文件,如.o. 3.用链接器连接目标代码生成可执行文件,如.exe. 但如果源文件太多,一个一个编译时 ...

  5. JS数组中级+高级技巧

    本文介绍JS数组一些比较进阶的方法: reverse:数组反转: join:(参数)以参数为连接符将数组拼接为字符串: 实例: var arr=[]; arr[3]="haha"; ...

  6. Redis 集群方案介绍

    由于Redis出众的性能,其在众多的移动互联网企业中得到广泛的应用.Redis在3.0版本前只支持单实例模式,虽然现在的服务器内存可以到100GB.200GB的规模,但是单实例模式限制了Redis没法 ...

  7. js中setAttribute 的兼容性

    js中setAttribute 的兼容性class和className兼容方法: object.setAttribute("class","content") ...

  8. 【小技能整理】mac vim开启语法高亮

    步骤1: cp /usr/share/vim/vimrc ~/.vimrc 先复制一份vim配置模板到个人目录下 注:redhat 改成 cp /etc/vimrc ~/.vimrc 步骤2: vi ...

  9. java对象模型

    java对象模型其实就是JVM中对象的内存布局.一个对象本身内在结构的描述信息以字节码的方式存储在方法区中(参见java内存区域),说白了就是class文件.那么如何获取到对象的class信息呢?虚拟 ...

  10. ORA-01652: 无法通过 128 (在表空间 TEMP 中) 扩展 temp 段(EXP-00056: 遇到 ORACLE 错误 1652 ORA-01652: unable to extend temp segment by 128 in tablespace TEMP)

    数据库报 ORA-01652: 无法通过 128 (在表空间 TEMP 中) 扩展 temp 段 两种解决方式: 第一种) sql>select * from v$tempfile; 发现tem ...