-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2808-MinimumSecondsToEqualizeACircularArray.go
More file actions
94 lines (78 loc) · 3.08 KB
/
2808-MinimumSecondsToEqualizeACircularArray.go
File metadata and controls
94 lines (78 loc) · 3.08 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
package main
import "fmt"
// 2808. Minimum Seconds to Equalize a Circular Array
// You are given a 0-indexed array nums containing n integers.
// At each second, you perform the following operation on the array:
// For every index i in the range [0, n - 1], replace nums[i] with either nums[i], nums[(i - 1 + n) % n], or nums[(i + 1) % n].
// Note that all the elements get replaced simultaneously.
// Return the minimum number of seconds needed to make all elements in the array nums equal.
// Example 1:
// Input: nums = [1,2,1,2]
// Output: 1
// Explanation: We can equalize the array in 1 second in the following way:
// - At 1st second, replace values at each index with [nums[3],nums[1],nums[3],nums[3]]. After replacement, nums = [2,2,2,2].
// It can be proven that 1 second is the minimum amount of seconds needed for equalizing the array.
// Example 2:
// Input: nums = [2,1,3,3,2]
// Output: 2
// Explanation: We can equalize the array in 2 seconds in the following way:
// - At 1st second, replace values at each index with [nums[0],nums[2],nums[2],nums[2],nums[3]]. After replacement, nums = [2,3,3,3,3].
// - At 2nd second, replace values at each index with [nums[1],nums[1],nums[2],nums[3],nums[4]]. After replacement, nums = [3,3,3,3,3].
// It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array.
// Example 3:
// Input: nums = [5,5,5,5]
// Output: 0
// Explanation: We don't need to perform any operations as all elements in the initial array are the same.
// Constraints:
// 1 <= n == nums.length <= 10^5
// 1 <= nums[i] <= 10^9
// 根据补码,其最大值二进制表示,首位0,其余1,那么,
const INT_MAX = int(^uint(0) >> 1)
// 根据补码,其最小值二进制表示,首位1,其余0,那么,
//const INT_MIN = ^INT_MAX
func minimumSeconds(nums []int) int {
// Create a mapping from each unique number to its indices in the array
l := len(nums)
m := make(map[int][]int, l)
for i := 0; i < l; i = i+1 {
if _, ok := m[nums[i]]; !ok {
m[nums[i]] = []int{}
}
m[nums[i]] = append(m[nums[i]], i)
}
//fmt.Println(m)
// Initialize the minimum number of seconds to a large value
minSeconds := INT_MAX; // use INT_MAX as shorthand for 1 << 30
// Iterate over the number-index mapping
for _, v := range m {
//fmt.Println(k,v)
vl := len(v)
// Compute initial distance considering the array as circular
maxDistance := v[0] + l - v[vl - 1];
// Loop over the indices to find the largest distance between any two consecutive indices
for i := 1; i < vl; i = i + 1 {
maxDistance = max(maxDistance, v[i] - v[i - 1]);
}
// Update the minimum number of seconds with the lower value
minSeconds = min(minSeconds, maxDistance / 2);
}
// Return the minimum number of seconds after completing the loop
return minSeconds;
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func min(a int, b int) int {
if a < b {
return a
}
return b
}
func main() {
fmt.Println(minimumSeconds([]int{ 1,2,1,2})) // 1
fmt.Println(minimumSeconds([]int{ 2,1,3,3,2})) // 2
fmt.Println(minimumSeconds([]int{ 5,5,5,5})) // 0
}