本文主要是介绍leetcode - 517. Super Washing Machines,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
动态规划,我尽力了…
class Solution {
public:int findMinMoves(vector<int>& machines) {int l(0),r(0),nextl(0),maxper(0), i, avr(0), n = machines.size();vector<int>::iterator it;for (it = machines.begin(); it != machines.end(); ++it){avr += *it;}if (avr%n) return -1;avr /= n;it = machines.begin();dp[0] = avr - *it;++it;if (dp[0]<0){r = -dp[0];}else {nextl = dp[0];}maxper=l+r;for (i = 1; it != machines.end(); ++it, ++i){l = nextl;dp[i] = avr - *it + dp[i - 1];if (dp[i]<0){r = -dp[i];nextl = 0;}else {r = 0;nextl = dp[i];}if (!*it){if (r) ++r;if (l) ++l;}maxper = max(maxper, l + r);
这篇关于leetcode - 517. Super Washing Machines的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!