[LeetCode OJ]Super Washing Machines

11 篇文章 0 订阅
7 篇文章 0 订阅

问题描述:

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 machines at the same time .

Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.

Example1

Input: [1,0,5]

Output: 3

Explanation: 
1st move:    1     0 <-- 5    =>    1     1     4
2nd move:    1 <-- 1 <-- 4    =>    2     1     3    
3rd move:    2     1 <-- 3    =>    2     2     2   

Example2

Input: [0,3,0]

Output: 2

Explanation: 
1st move:    0 <-- 3     0    =>    1     2     0    
2nd move:    1     2 --> 0    =>    1     1     1     

Example3

Input: [0,2,0]

Output: -1

Explanation: 
It's impossible to make all the three washing machines have the same number of dresses. 

Note:

  1. The range of n is [1, 10000].
  2. The range of dresses number in a super washing machine is [0, 1e5].
问题来源:Super Washing Machines

解题分析:

问题相当于负载均衡,确保每一个节点能得到同等规模的样本数据。如果不能负载均衡,则返回-1;如果样本数据为空,则返回0。

考虑这里位置互不相关,可以同时进行操作,我们只需要能获取任意位置上所需最少操作数,这样遍历整个数组空间,便能得到整个数组空间操作数的最小数。

定义averge为每个洗衣机应该拥有的衣服数量,size为洗衣机的数量。对于位置i上的洗衣机来说(从下标0开始),它左边应该有averge * i件衣服,如果小于这个数,表明位置i左边的洗衣机整体需要从右边(包括位置i)洗衣机整体中获取衣服,而获取衣服必须要通过位置为i的洗衣机,右边也同理。

定义left_to_right为位置i左边的洗衣机应该向右边(包括位置i)洗衣机输送的衣服数量,right_to_left为位置i右边向左边(包括位置i)输送的衣服数量,此时可判断left_to_left和right_to_left的正负来确定位置i上的最小操作数;

1.如果left_to_right和right_to_left都小于0,说明两边都需要从位置i拿取衣服,这时不可同时进行,最小操作数为-(left_to_right+right_to_left);
2.如果left_to_right和right_to_left都大于0,说明两边都需要向位置i输送衣服,这时可同时进行,最小操作数为max(left_to_left,  right_to_left);
3.其它情况则是一边需要输送衣服,另一边需要拿取衣服,可同时进行,最小操作数为max(abs(left_to_left), abs(right_to_left));

遍历整个数组空间,便能得到整个数组空间的最小的操作数,时间复杂度为O(N)。

源代码:

class Solution {
public:
    int findMinMoves(vector<int>& machines) {
    	int size = machines.size();
    	if (size == 0) {
    		return 0;
    	}
    	vector<int> sum(size + 1, 0);
        for (int i = 0; i < size; i++) {
        	/*前面i个洗衣机拥有的衣服的总量*/
        	sum[i+1] = machines[i] + sum[i];
        }
        if (sum[size] % size != 0) {
        	return -1;
        }
        /*每个洗衣机应该拥有的衣服数量*/
        int averge = sum[size] / size;
        /*整个数组空间所需最少操作数*/
        int minMoves = 0;
        /*从左边向右边输送的衣服数*/
        int left_to_right = 0;
        /*从右边向左边输送的衣服数*/
        int right_to_left = 0;

        for (int i = 0; i < size; i++) {
        	left_to_right = sum[i] - averge * i;
        	right_to_left = sum[size] - averge * (size - i - 1) - sum[i+1];
        	/*两边都需要从第i位置获取衣服*/
        	if (left_to_right < 0 && right_to_left < 0) {
        		minMoves = max(minMoves, -left_to_right - right_to_left);
        	} 
        	/*两边都需要向第i位置输送衣服*/
        	else if(left_to_right > 0 && right_to_left > 0){
        		minMoves = max(minMoves, max(left_to_right, right_to_left));
        	} else {
        		minMoves = max(minMoves, max(abs(left_to_right), abs(right_to_left)));
        	}
        }
        return minMoves;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值