-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimize-deviation-in-array.go
117 lines (99 loc) · 1.84 KB
/
minimize-deviation-in-array.go
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
package main
import (
"container/heap"
"fmt"
)
// source: https://leetcode.com/problems/minimize-deviation-in-array/
type IntHeap []int
func NewIntHeap(values ...int) *IntHeap {
h := IntHeap(values)
heap.Init(&h)
return &h
}
func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] > h[j] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}
func (h *IntHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}
func (h IntHeap) Peek() int {
return h[0]
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
func minimumDeviation(nums []int) int {
minVal := 1<<31 - 1
for i := range nums {
if nums[i]%2 == 1 {
nums[i] *= 2
}
minVal = min(minVal, nums[i])
}
pq := NewIntHeap(nums...)
ans := pq.Peek() - minVal
for pq.Peek()%2 == 0 {
x := heap.Pop(pq).(int)
x /= 2
minVal = min(minVal, x)
heap.Push(pq, x)
ans = min(ans, pq.Peek()-minVal)
}
return ans
}
func main() {
testCases := []struct {
nums []int
want int
}{
{
nums: []int{3, 5},
want: 1,
},
{
nums: []int{10, 4, 3},
want: 2,
},
{
nums: []int{4, 1, 5, 20, 3},
want: 3,
},
{
nums: []int{1, 11},
want: 9,
},
{
nums: []int{2, 10, 8},
want: 3,
},
{
nums: []int{1, 2, 3, 4},
want: 1,
},
}
successes := 0
for _, tc := range testCases {
x := minimumDeviation(tc.nums)
status := "ERROR"
if fmt.Sprint(x) == fmt.Sprint(tc.want) {
status = "OK"
successes++
}
fmt.Println(status, " Expected: ", tc.want, " Actual: ", x)
}
if l := len(testCases); successes == len(testCases) {
fmt.Printf("===\nSUCCESS: %d of %d tests ended successfully\n", successes, l)
} else {
fmt.Printf("===\nFAIL: %d tests failed\n", l-successes)
}
}