File size: 2,114 Bytes
3c88a0f | 1 2 3 4 5 | {"type": "session", "harness": "opencode", "id": "cp-10024", "name": "LeetCode 0517: Super Washing Machines"}
{"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Super Washing Machines (Difficulty: Hard)\nTags: Array, Greedy\n\nYou have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.\nFor 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.\nGiven an integer array machines representing the number of dresses in each washing machine from left to right on the line, return 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.\n\nExample 1:\nInput: machines = [1,0,5]\nOutput: 3\nExplanation:\n1st move: 1 0 <-- 5 => 1 1 4\n2nd move: 1 <-- 1 <-- 4 => 2 1 3\n3rd move: 2 1 <-- 3 => 2 2 2\n\nExample 2:\nInput: machines = [0,3,0]\nOutput: 2\nExplanation:\n1st move: 0 <-- 3 0 => 1 2 0\n2nd move: 1 2 --> 0 => 1 1 1\n\nExample 3:\nInput: machines = [0,2,0]\nOutput: -1\nExplanation:\nIt's impossible to make all three washing machines have the same number of dresses.\n\nConstraints:\n- n == machines.length\n- 1 <= n <= 10^4\n- 0 <= machines[i] <= 10^5"}}
{"type": "message", "message": {"role": "assistant", "content": "# Time: O(n)\n# Space: O(1)\n\nclass Solution(object):\n def findMinMoves(self, machines):\n \"\"\"\n :type machines: List[int]\n :rtype: int\n \"\"\"\n total = sum(machines)\n if total % len(machines): return -1\n\n result, target, curr = 0, total / len(machines), 0\n for n in machines:\n curr += n - target\n result = max(result, max(n - target, abs(curr)))\n return result\n\n"}}
|