|
58 | 58 |
|
59 | 59 | <!-- 这里可写通用的实现逻辑 -->
|
60 | 60 |
|
| 61 | +**方法一:贪心** |
| 62 | + |
| 63 | +如果洗衣机内的衣服总数不能被洗衣机的数量整除,那么不可能使得每台洗衣机内的衣服数量相等,直接返回 $-1$。 |
| 64 | + |
| 65 | +否则,假设洗衣机内的衣服总数为 $s$,那么最终每台洗衣机内的衣服数量都会变为 $k = s / n$。 |
| 66 | + |
| 67 | +我们定义 $a_i$ 为第 $i$ 台洗衣机内的衣服数量与 $k$ 的差值,即 $a_i = \text{machines}[i] - k$。若 $a_i > 0$,则表示第 $i$ 台洗衣机内有多余的衣服,需要向相邻的洗衣机传递;若 $a_i < 0$,则表示第 $i$ 台洗衣机内缺少衣服,需要从相邻的洗衣机获得。 |
| 68 | + |
| 69 | +我们将前 $i$ 台洗衣机的衣服数量差值之和定义为 $s_i = \sum_{j=0}^{i-1} a_j$,如果把前 $i$ 台洗衣机视为一组,其余的洗衣机视为另一组。那么若 $s_i$ 为正数,表示第一组洗衣机内有多余的衣服,需要向第二组洗衣机传递;若 $s_i$ 为负数,表示第一组洗衣机内缺少衣服,需要从第二组洗衣机获得。 |
| 70 | + |
| 71 | +那么有以下两种情况: |
| 72 | + |
| 73 | +1. 两组之间的衣服,最多需要移动的次数为 $\max_{i=0}^{n-1} \lvert s_i \rvert$; |
| 74 | +1. 组内某一台洗衣机的衣服数量过多,需要向左右两侧移出衣服,最多需要移动的次数为 $\max_{i=0}^{n-1} a_i$。 |
| 75 | + |
| 76 | +我们取两者的最大值即可。 |
| 77 | + |
| 78 | +时间复杂度 $O(n)$,其中 $n$ 为洗衣机的数量。空间复杂度 $O(1)$。 |
| 79 | + |
61 | 80 | <!-- tabs:start -->
|
62 | 81 |
|
63 | 82 | ### **Python3**
|
64 | 83 |
|
65 | 84 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 85 |
|
67 | 86 | ```python
|
68 |
| - |
| 87 | +class Solution: |
| 88 | + def findMinMoves(self, machines: List[int]) -> int: |
| 89 | + n = len(machines) |
| 90 | + k, mod = divmod(sum(machines), n) |
| 91 | + if mod: |
| 92 | + return -1 |
| 93 | + ans = s = 0 |
| 94 | + for x in machines: |
| 95 | + x -= k |
| 96 | + s += x |
| 97 | + ans = max(ans, abs(s), x) |
| 98 | + return ans |
69 | 99 | ```
|
70 | 100 |
|
71 | 101 | ### **Java**
|
72 | 102 |
|
73 | 103 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
74 | 104 |
|
75 | 105 | ```java
|
| 106 | +class Solution { |
| 107 | + public int findMinMoves(int[] machines) { |
| 108 | + int n = machines.length; |
| 109 | + int s = 0; |
| 110 | + for (int x : machines) { |
| 111 | + s += x; |
| 112 | + } |
| 113 | + if (s % n != 0) { |
| 114 | + return -1; |
| 115 | + } |
| 116 | + int k = s / n; |
| 117 | + s = 0; |
| 118 | + int ans = 0; |
| 119 | + for (int x : machines) { |
| 120 | + x -= k; |
| 121 | + s += x; |
| 122 | + ans = Math.max(ans, Math.max(Math.abs(s), x)); |
| 123 | + } |
| 124 | + return ans; |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### **C++** |
| 130 | + |
| 131 | +```cpp |
| 132 | +class Solution { |
| 133 | +public: |
| 134 | + int findMinMoves(vector<int>& machines) { |
| 135 | + int n = machines.size(); |
| 136 | + int s = accumulate(machines.begin(), machines.end(), 0); |
| 137 | + if (s % n) { |
| 138 | + return -1; |
| 139 | + } |
| 140 | + int k = s / n; |
| 141 | + s = 0; |
| 142 | + int ans = 0; |
| 143 | + for (int x : machines) { |
| 144 | + x -= k; |
| 145 | + s += x; |
| 146 | + ans = max({ans, abs(s), x}); |
| 147 | + } |
| 148 | + return ans; |
| 149 | + } |
| 150 | +}; |
| 151 | +``` |
| 152 | +
|
| 153 | +### **Go** |
| 154 | +
|
| 155 | +```go |
| 156 | +func findMinMoves(machines []int) (ans int) { |
| 157 | + n := len(machines) |
| 158 | + s := 0 |
| 159 | + for _, x := range machines { |
| 160 | + s += x |
| 161 | + } |
| 162 | + if s%n != 0 { |
| 163 | + return -1 |
| 164 | + } |
| 165 | + k := s / n |
| 166 | + s = 0 |
| 167 | + for _, x := range machines { |
| 168 | + x -= k |
| 169 | + s += x |
| 170 | + ans = max(ans, max(abs(s), x)) |
| 171 | + } |
| 172 | + return |
| 173 | +} |
| 174 | +
|
| 175 | +func max(a, b int) int { |
| 176 | + if a > b { |
| 177 | + return a |
| 178 | + } |
| 179 | + return b |
| 180 | +} |
| 181 | +
|
| 182 | +func abs(x int) int { |
| 183 | + if x < 0 { |
| 184 | + return -x |
| 185 | + } |
| 186 | + return x |
| 187 | +} |
| 188 | +``` |
76 | 189 |
|
| 190 | +### **TypeScript** |
| 191 | + |
| 192 | +```ts |
| 193 | +function findMinMoves(machines: number[]): number { |
| 194 | + const n = machines.length; |
| 195 | + let s = machines.reduce((a, b) => a + b); |
| 196 | + if (s % n !== 0) { |
| 197 | + return -1; |
| 198 | + } |
| 199 | + const k = Math.floor(s / n); |
| 200 | + s = 0; |
| 201 | + let ans = 0; |
| 202 | + for (let x of machines) { |
| 203 | + x -= k; |
| 204 | + s += x; |
| 205 | + ans = Math.max(ans, Math.abs(s), x); |
| 206 | + } |
| 207 | + return ans; |
| 208 | +} |
77 | 209 | ```
|
78 | 210 |
|
79 | 211 | ### **...**
|
|
0 commit comments