Skip to content

feat: add solutions to lc problem: No.0517 #1589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 133 additions & 1 deletion solution/0500-0599/0517.Super Washing Machines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,154 @@

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

**方法一:贪心**

如果洗衣机内的衣服总数不能被洗衣机的数量整除,那么不可能使得每台洗衣机内的衣服数量相等,直接返回 $-1$。

否则,假设洗衣机内的衣服总数为 $s$,那么最终每台洗衣机内的衣服数量都会变为 $k = s / n$。

我们定义 $a_i$ 为第 $i$ 台洗衣机内的衣服数量与 $k$ 的差值,即 $a_i = \text{machines}[i] - k$。若 $a_i > 0$,则表示第 $i$ 台洗衣机内有多余的衣服,需要向相邻的洗衣机传递;若 $a_i < 0$,则表示第 $i$ 台洗衣机内缺少衣服,需要从相邻的洗衣机获得。

我们将前 $i$ 台洗衣机的衣服数量差值之和定义为 $s_i = \sum_{j=0}^{i-1} a_j$,如果把前 $i$ 台洗衣机视为一组,其余的洗衣机视为另一组。那么若 $s_i$ 为正数,表示第一组洗衣机内有多余的衣服,需要向第二组洗衣机传递;若 $s_i$ 为负数,表示第一组洗衣机内缺少衣服,需要从第二组洗衣机获得。

那么有以下两种情况:

1. 两组之间的衣服,最多需要移动的次数为 $\max_{i=0}^{n-1} \lvert s_i \rvert$;
1. 组内某一台洗衣机的衣服数量过多,需要向左右两侧移出衣服,最多需要移动的次数为 $\max_{i=0}^{n-1} a_i$。

我们取两者的最大值即可。

时间复杂度 $O(n)$,其中 $n$ 为洗衣机的数量。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def findMinMoves(self, machines: List[int]) -> int:
n = len(machines)
k, mod = divmod(sum(machines), n)
if mod:
return -1
ans = s = 0
for x in machines:
x -= k
s += x
ans = max(ans, abs(s), x)
return ans
```

### **Java**

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

```java
class Solution {
public int findMinMoves(int[] machines) {
int n = machines.length;
int s = 0;
for (int x : machines) {
s += x;
}
if (s % n != 0) {
return -1;
}
int k = s / n;
s = 0;
int ans = 0;
for (int x : machines) {
x -= k;
s += x;
ans = Math.max(ans, Math.max(Math.abs(s), x));
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int findMinMoves(vector<int>& machines) {
int n = machines.size();
int s = accumulate(machines.begin(), machines.end(), 0);
if (s % n) {
return -1;
}
int k = s / n;
s = 0;
int ans = 0;
for (int x : machines) {
x -= k;
s += x;
ans = max({ans, abs(s), x});
}
return ans;
}
};
```

### **Go**

```go
func findMinMoves(machines []int) (ans int) {
n := len(machines)
s := 0
for _, x := range machines {
s += x
}
if s%n != 0 {
return -1
}
k := s / n
s = 0
for _, x := range machines {
x -= k
s += x
ans = max(ans, max(abs(s), x))
}
return
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

### **TypeScript**

```ts
function findMinMoves(machines: number[]): number {
const n = machines.length;
let s = machines.reduce((a, b) => a + b);
if (s % n !== 0) {
return -1;
}
const k = Math.floor(s / n);
s = 0;
let ans = 0;
for (let x of machines) {
x -= k;
s += x;
ans = Math.max(ans, Math.abs(s), x);
}
return ans;
}
```

### **...**
Expand Down
115 changes: 114 additions & 1 deletion solution/0500-0599/0517.Super Washing Machines/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,126 @@ It&#39;s impossible to make all three washing machines have the same number of d
### **Python3**

```python

class Solution:
def findMinMoves(self, machines: List[int]) -> int:
n = len(machines)
k, mod = divmod(sum(machines), n)
if mod:
return -1
ans = s = 0
for x in machines:
x -= k
s += x
ans = max(ans, abs(s), x)
return ans
```

### **Java**

```java
class Solution {
public int findMinMoves(int[] machines) {
int n = machines.length;
int s = 0;
for (int x : machines) {
s += x;
}
if (s % n != 0) {
return -1;
}
int k = s / n;
s = 0;
int ans = 0;
for (int x : machines) {
x -= k;
s += x;
ans = Math.max(ans, Math.max(Math.abs(s), x));
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int findMinMoves(vector<int>& machines) {
int n = machines.size();
int s = accumulate(machines.begin(), machines.end(), 0);
if (s % n) {
return -1;
}
int k = s / n;
s = 0;
int ans = 0;
for (int x : machines) {
x -= k;
s += x;
ans = max({ans, abs(s), x});
}
return ans;
}
};
```

### **Go**

```go
func findMinMoves(machines []int) (ans int) {
n := len(machines)
s := 0
for _, x := range machines {
s += x
}
if s%n != 0 {
return -1
}
k := s / n
s = 0
for _, x := range machines {
x -= k
s += x
ans = max(ans, max(abs(s), x))
}
return
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

### **TypeScript**

```ts
function findMinMoves(machines: number[]): number {
const n = machines.length;
let s = machines.reduce((a, b) => a + b);
if (s % n !== 0) {
return -1;
}
const k = Math.floor(s / n);
s = 0;
let ans = 0;
for (let x of machines) {
x -= k;
s += x;
ans = Math.max(ans, Math.abs(s), x);
}
return ans;
}
```

### **...**
Expand Down
19 changes: 19 additions & 0 deletions solution/0500-0599/0517.Super Washing Machines/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int findMinMoves(vector<int>& machines) {
int n = machines.size();
int s = accumulate(machines.begin(), machines.end(), 0);
if (s % n) {
return -1;
}
int k = s / n;
s = 0;
int ans = 0;
for (int x : machines) {
x -= k;
s += x;
ans = max({ans, abs(s), x});
}
return ans;
}
};
32 changes: 32 additions & 0 deletions solution/0500-0599/0517.Super Washing Machines/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
func findMinMoves(machines []int) (ans int) {
n := len(machines)
s := 0
for _, x := range machines {
s += x
}
if s%n != 0 {
return -1
}
k := s / n
s = 0
for _, x := range machines {
x -= k
s += x
ans = max(ans, max(abs(s), x))
}
return
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
21 changes: 21 additions & 0 deletions solution/0500-0599/0517.Super Washing Machines/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int findMinMoves(int[] machines) {
int n = machines.length;
int s = 0;
for (int x : machines) {
s += x;
}
if (s % n != 0) {
return -1;
}
int k = s / n;
s = 0;
int ans = 0;
for (int x : machines) {
x -= k;
s += x;
ans = Math.max(ans, Math.max(Math.abs(s), x));
}
return ans;
}
}
12 changes: 12 additions & 0 deletions solution/0500-0599/0517.Super Washing Machines/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def findMinMoves(self, machines: List[int]) -> int:
n = len(machines)
k, mod = divmod(sum(machines), n)
if mod:
return -1
ans = s = 0
for x in machines:
x -= k
s += x
ans = max(ans, abs(s), x)
return ans
16 changes: 16 additions & 0 deletions solution/0500-0599/0517.Super Washing Machines/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function findMinMoves(machines: number[]): number {
const n = machines.length;
let s = machines.reduce((a, b) => a + b);
if (s % n !== 0) {
return -1;
}
const k = Math.floor(s / n);
s = 0;
let ans = 0;
for (let x of machines) {
x -= k;
s += x;
ans = Math.max(ans, Math.abs(s), x);
}
return ans;
}