O(n) time O(1) space python


  • 0
    D
    class Solution(object):
        def findMinMoves(self, machines):
            """
            :type machines: List[int]
            :rtype: int
            """
            total = sum(machines)
            if total%len(machines)!=0:
                return -1
            ave = total/len(machines)
            prefix = 0
            res = 0
            for i in range(len(machines)):
                leftNeeds = max(i*ave - prefix, 0)
                rightNeeds = max(prefix + machines[i] - (i+1)*ave, 0)
                prefix += machines[i]
                res = max(leftNeeds+rightNeeds, res)
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.