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

今天开始定期记录本人在leetcode上刷题时遇到的有意思的题目.   517. Super Washing Machines   You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass…
▶ 超级洗碗机.给定一个有 n 元素的整数数组,我们把 “将指定位置上元素的值减 1,同时其左侧或者右侧相邻元素的值加 1” 称为一次操作,每个回合内,可以选定任意 1 至 n 个位置进行独立的操作,求最少的回合数,使得该数组中的搜有元素调整为相等的值.若不存在(所有元素的和不能被元素个数整除),返回 -1 . ● 代码,14 ms,最大差距法.考虑将所有元素的平均值平移到 0,记此时 machines[ 0 ] == a,如果 a == 0,则 machines[ 0 ]已经调整完成,跳过:若…
详见:https://leetcode.com/problems/super-washing-machines/description/ C++: class Solution { public: int findMinMoves(vector<int>& machines) { int sum = accumulate(machines.begin(), machines.end(), 0); if (sum % machines.size() != 0) { return -1;…
Leetcode517 很有趣的一道题 由于每一步可以任选某些数字对它们进行转移,所以实际上是在求最优解中的最复杂转移数. 那么我们考虑,到底哪一个位置要经过的流量最大呢? 枚举每个位置,考虑它左边的整体需求和右边的整体需求,如果两边都需要流入,则流量相加: 如果一边需要流入,一边需要流出,则取绝对值的最大值. 复杂度O(n) class Solution { public: int findMinMoves(vector<int>& machines) { int len = mac…
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing m…
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing m…
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing m…
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对应的随笔下面评论区留言,我会及时处理,在此谢过了. 过程或许会很漫长,也很痛苦,慢慢来吧. 编号 题名 过题率 难度 1 Two Sum 0.376 Easy 2 Add Two Numbers 0.285 Medium 3 Longest Substring Without Repeating C…
1. 520. Detect Capital 题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断.还有:我感觉自己写的代码很丑,判断条件比较多,需要改进,精简再精简. class Solution { public: bool detectCapitalUse(string word) { int n = word.size(); ) ; ] <= 'Z') { ; ; i < n; i++) { if(word[i] >= 'a') {}…
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017) .   Top Interview Questions # Title Difficulty Acceptance 1 Two Sum Medium 17.70% 2 Add Two N…