Skip to content

Commit 408589c

Browse files
authored
feat: add solutions to lc problem: No.0517 (doocs#1589)
No.0517.Super Washing Machines
1 parent 0ad4f70 commit 408589c

File tree

7 files changed

+347
-2
lines changed

7 files changed

+347
-2
lines changed

solution/0500-0599/0517.Super Washing Machines/README.md

+133-1
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,154 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

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+
6180
<!-- tabs:start -->
6281

6382
### **Python3**
6483

6584
<!-- 这里可写当前语言的特殊实现逻辑 -->
6685

6786
```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
6999
```
70100

71101
### **Java**
72102

73103
<!-- 这里可写当前语言的特殊实现逻辑 -->
74104

75105
```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+
```
76189

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+
}
77209
```
78210

79211
### **...**

solution/0500-0599/0517.Super Washing Machines/README_EN.md

+114-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,126 @@ It&#39;s impossible to make all three washing machines have the same number of d
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def findMinMoves(self, machines: List[int]) -> int:
62+
n = len(machines)
63+
k, mod = divmod(sum(machines), n)
64+
if mod:
65+
return -1
66+
ans = s = 0
67+
for x in machines:
68+
x -= k
69+
s += x
70+
ans = max(ans, abs(s), x)
71+
return ans
6172
```
6273

6374
### **Java**
6475

6576
```java
77+
class Solution {
78+
public int findMinMoves(int[] machines) {
79+
int n = machines.length;
80+
int s = 0;
81+
for (int x : machines) {
82+
s += x;
83+
}
84+
if (s % n != 0) {
85+
return -1;
86+
}
87+
int k = s / n;
88+
s = 0;
89+
int ans = 0;
90+
for (int x : machines) {
91+
x -= k;
92+
s += x;
93+
ans = Math.max(ans, Math.max(Math.abs(s), x));
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int findMinMoves(vector<int>& machines) {
106+
int n = machines.size();
107+
int s = accumulate(machines.begin(), machines.end(), 0);
108+
if (s % n) {
109+
return -1;
110+
}
111+
int k = s / n;
112+
s = 0;
113+
int ans = 0;
114+
for (int x : machines) {
115+
x -= k;
116+
s += x;
117+
ans = max({ans, abs(s), x});
118+
}
119+
return ans;
120+
}
121+
};
122+
```
123+
124+
### **Go**
125+
126+
```go
127+
func findMinMoves(machines []int) (ans int) {
128+
n := len(machines)
129+
s := 0
130+
for _, x := range machines {
131+
s += x
132+
}
133+
if s%n != 0 {
134+
return -1
135+
}
136+
k := s / n
137+
s = 0
138+
for _, x := range machines {
139+
x -= k
140+
s += x
141+
ans = max(ans, max(abs(s), x))
142+
}
143+
return
144+
}
145+
146+
func max(a, b int) int {
147+
if a > b {
148+
return a
149+
}
150+
return b
151+
}
152+
153+
func abs(x int) int {
154+
if x < 0 {
155+
return -x
156+
}
157+
return x
158+
}
159+
```
66160

161+
### **TypeScript**
162+
163+
```ts
164+
function findMinMoves(machines: number[]): number {
165+
const n = machines.length;
166+
let s = machines.reduce((a, b) => a + b);
167+
if (s % n !== 0) {
168+
return -1;
169+
}
170+
const k = Math.floor(s / n);
171+
s = 0;
172+
let ans = 0;
173+
for (let x of machines) {
174+
x -= k;
175+
s += x;
176+
ans = Math.max(ans, Math.abs(s), x);
177+
}
178+
return ans;
179+
}
67180
```
68181

69182
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int findMinMoves(vector<int>& machines) {
4+
int n = machines.size();
5+
int s = accumulate(machines.begin(), machines.end(), 0);
6+
if (s % n) {
7+
return -1;
8+
}
9+
int k = s / n;
10+
s = 0;
11+
int ans = 0;
12+
for (int x : machines) {
13+
x -= k;
14+
s += x;
15+
ans = max({ans, abs(s), x});
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func findMinMoves(machines []int) (ans int) {
2+
n := len(machines)
3+
s := 0
4+
for _, x := range machines {
5+
s += x
6+
}
7+
if s%n != 0 {
8+
return -1
9+
}
10+
k := s / n
11+
s = 0
12+
for _, x := range machines {
13+
x -= k
14+
s += x
15+
ans = max(ans, max(abs(s), x))
16+
}
17+
return
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}
26+
27+
func abs(x int) int {
28+
if x < 0 {
29+
return -x
30+
}
31+
return x
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int findMinMoves(int[] machines) {
3+
int n = machines.length;
4+
int s = 0;
5+
for (int x : machines) {
6+
s += x;
7+
}
8+
if (s % n != 0) {
9+
return -1;
10+
}
11+
int k = s / n;
12+
s = 0;
13+
int ans = 0;
14+
for (int x : machines) {
15+
x -= k;
16+
s += x;
17+
ans = Math.max(ans, Math.max(Math.abs(s), x));
18+
}
19+
return ans;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def findMinMoves(self, machines: List[int]) -> int:
3+
n = len(machines)
4+
k, mod = divmod(sum(machines), n)
5+
if mod:
6+
return -1
7+
ans = s = 0
8+
for x in machines:
9+
x -= k
10+
s += x
11+
ans = max(ans, abs(s), x)
12+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function findMinMoves(machines: number[]): number {
2+
const n = machines.length;
3+
let s = machines.reduce((a, b) => a + b);
4+
if (s % n !== 0) {
5+
return -1;
6+
}
7+
const k = Math.floor(s / n);
8+
s = 0;
9+
let ans = 0;
10+
for (let x of machines) {
11+
x -= k;
12+
s += x;
13+
ans = Math.max(ans, Math.abs(s), x);
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
 (0)