-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path517-SuperWashingMachines.go
More file actions
125 lines (113 loc) · 3.96 KB
/
517-SuperWashingMachines.go
File metadata and controls
125 lines (113 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
// 517. Super Washing Machines
// You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
// For 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.
// Given 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.
// Example 1:
// Input: machines = [1,0,5]
// Output: 3
// Explanation:
// 1st move: 1 0 <-- 5 => 1 1 4
// 2nd move: 1 <-- 1 <-- 4 => 2 1 3
// 3rd move: 2 1 <-- 3 => 2 2 2
// Example 2:
// Input: machines = [0,3,0]
// Output: 2
// Explanation:
// 1st move: 0 <-- 3 0 => 1 2 0
// 2nd move: 1 2 --> 0 => 1 1 1
// Example 3:
// Input: machines = [0,2,0]
// Output: -1
// Explanation:
// It's impossible to make all three washing machines have the same number of dresses.
// Constraints:
// n == machines.length
// 1 <= n <= 10^4
// 0 <= machines[i] <= 10^5
import "fmt"
func findMinMoves(machines []int) int {
//since each machine can only transfer one dress at each step
//the strategy is to find for each machine, the sum of dresses that passes left/right through this machine
//among all the machine, the highest passes through number is also the steps to take
sum, n := 0, len(machines)
for _, v := range machines { // 汇总
sum +=v
}
if sum % n != 0 { // 不能被平分
return -1
}
max := func (x, y int) int { if x > y { return x; }; return y; }
res, avg, leftSum := 0, sum / n, 0
leftTarget, rightTarget := 0, sum + avg
for _, v := range machines {
leftSum += v // prefix sum, i included
rightSum := sum - leftSum + v // suffix sum, i included
leftTarget += avg // prefix target, i included
rightTarget -= avg // suffix target, i included
toRight, toLeft := 0, 0
if leftSum > leftTarget {
toRight = leftSum - leftTarget
}
if rightSum > rightTarget {
toLeft = rightSum - rightTarget
}
res = max(res, toLeft + toRight)
}
return res
}
func findMinMoves1(machines []int) int {
sum, n := 0, len(machines)
for _, v := range machines { // 汇总
sum += v
}
if sum % n != 0{
return -1
}
res, avg := 0, sum / n
for i := 0; i < n; i++ {
machines[i] = machines[i] - avg
if machines[i] > 0 && res < machines[i] {
res = machines[i]
}
}
for i := 1; i < n; i++ {
machines[i] += machines[i-1]
if machines[i] < 0 && res < -machines[i] {
res = -machines[i]
}
if machines[i] > 0 && res < machines[i] {
res = machines[i]
}
}
return res
}
func main() {
// Example 1:
// Input: machines = [1,0,5]
// Output: 3
// Explanation:
// 1st move: 1 0 <-- 5 => 1 1 4
// 2nd move: 1 <-- 1 <-- 4 => 2 1 3
// 3rd move: 2 1 <-- 3 => 2 2 2
fmt.Println(findMinMoves([]int{1,0,5})) // 3
// Example 2:
// Input: machines = [0,3,0]
// Output: 2
// Explanation:
// 1st move: 0 <-- 3 0 => 1 2 0
// 2nd move: 1 2 --> 0 => 1 1 1
fmt.Println(findMinMoves([]int{0,3,0})) // 2
// Example 3:
// Input: machines = [0,2,0]
// Output: -1
// Explanation:
// It's impossible to make all three washing machines have the same number of dresses.
fmt.Println(findMinMoves([]int{0,2,0})) // -1
fmt.Println(findMinMoves1([]int{1,0,5})) // 3
fmt.Println(findMinMoves1([]int{0,3,0})) // 2
fmt.Println(findMinMoves1([]int{0,2,0})) // -1
}