-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3880-MinimumAbsoluteDifferenceBetweenTwoValues.go
More file actions
78 lines (67 loc) · 2.35 KB
/
3880-MinimumAbsoluteDifferenceBetweenTwoValues.go
File metadata and controls
78 lines (67 loc) · 2.35 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
package main
// 3880. Minimum Absolute Difference Between Two Values
// You are given an integer array nums consisting only of 0, 1, and 2.
// A pair of indices (i, j) is called valid if nums[i] == 1 and nums[j] == 2.
// Return the minimum absolute difference between i and j among all valid pairs. If no valid pair exists, return -1.
// The absolute difference between indices i and j is defined as abs(i - j).
// Example 1:
// Input: nums = [1,0,0,2,0,1]
// Output: 2
// Explanation:
// The valid pairs are:
// (0, 3) which has absolute difference of abs(0 - 3) = 3.
// (5, 3) which has absolute difference of abs(5 - 3) = 2.
// Thus, the answer is 2.
// Example 2:
// Input: nums = [1,0,1,0]
// Output: -1
// Explanation:
// There are no valid pairs in the array, thus the answer is -1.
// Constraints:
// 1 <= nums.length <= 100
// 0 <= nums[i] <= 2
import "fmt"
func minAbsoluteDifference(nums []int) int {
res, one, two := 1 << 61, -1, -1
for i, v := range nums {
if v == 1 {
one = i
if two > -1 {
res = min(res, one - two)
}
}
if v == 2 {
two = i
if one > -1 {
res = min(res, two - one)
}
}
}
if res == 1 << 61 { return -1 }
return res
}
func main() {
// Example 1:
// Input: nums = [1,0,0,2,0,1]
// Output: 2
// Explanation:
// The valid pairs are:
// (0, 3) which has absolute difference of abs(0 - 3) = 3.
// (5, 3) which has absolute difference of abs(5 - 3) = 2.
// Thus, the answer is 2.
fmt.Println(minAbsoluteDifference([]int{1,0,0,2,0,1})) // 2
// Example 2:
// Input: nums = [1,0,1,0]
// Output: -1
// Explanation:
// There are no valid pairs in the array, thus the answer is -1.
fmt.Println(minAbsoluteDifference([]int{1,0,1,0})) // -1
fmt.Println(minAbsoluteDifference([]int{0,0,0,0,0,0,0,0,0})) // -1
fmt.Println(minAbsoluteDifference([]int{1,1,1,1,1,1,1,1,1})) // -1
fmt.Println(minAbsoluteDifference([]int{2,2,2,2,2,2,2,2,2})) // -1
fmt.Println(minAbsoluteDifference([]int{0,0,0,1,1,1,2,2,2})) // 1
fmt.Println(minAbsoluteDifference([]int{1,1,1,2,2,2,0,0,0})) // 1
fmt.Println(minAbsoluteDifference([]int{0})) // -1
fmt.Println(minAbsoluteDifference([]int{1})) // -1
fmt.Println(minAbsoluteDifference([]int{2})) // -1
}