-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2817-MinimumAbsoluteDifferenceBetweenElementsWithConstraint.go
More file actions
110 lines (97 loc) · 4.28 KB
/
2817-MinimumAbsoluteDifferenceBetweenElementsWithConstraint.go
File metadata and controls
110 lines (97 loc) · 4.28 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
package main
// 2817. Minimum Absolute Difference Between Elements With Constraint
// You are given a 0-indexed integer array nums and an integer x.
// Find the minimum absolute difference between two elements in the array that are at least x indices apart.
// In other words, find two indices i and j such that abs(i - j) >= x and abs(nums[i] - nums[j]) is minimized.
// Return an integer denoting the minimum absolute difference between two elements that are at least x indices apart.
// Example 1:
// Input: nums = [4,3,2,4], x = 2
// Output: 0
// Explanation: We can select nums[0] = 4 and nums[3] = 4.
// They are at least 2 indices apart, and their absolute difference is the minimum, 0.
// It can be shown that 0 is the optimal answer.
// Example 2:
// Input: nums = [5,3,2,10,15], x = 1
// Output: 1
// Explanation: We can select nums[1] = 3 and nums[2] = 2.
// They are at least 1 index apart, and their absolute difference is the minimum, 1.
// It can be shown that 1 is the optimal answer.
// Example 3:
// Input: nums = [1,2,3,4], x = 3
// Output: 3
// Explanation: We can select nums[0] = 1 and nums[3] = 4.
// They are at least 3 indices apart, and their absolute difference is the minimum, 3.
// It can be shown that 3 is the optimal answer.
// Constraints:
// 1 <= nums.length <= 10^5
// 1 <= nums[i] <= 10^9
// 0 <= x < nums.length
import "fmt"
import "sort"
func minAbsoluteDifference(nums []int, x int) int {
if x == 0 { return 0 } // optimisation
res, n := 1 << 31, len(nums)
sortedList := make([]int, 0, n)
abs := func(x int) int { if x < 0 { return -x; }; return x; }
min := func (x, y int) int { if x < y { return x; }; return y; }
for i := x; i < n; i++ {
num, newEl := nums[i], nums[i - x]
// insert new element, so 'sortedList' contains all elements in range [0, i-x] in sorted order
ln := len(sortedList)
insertIdx := sort.Search(ln, func(j int) bool { return sortedList[j] >= newEl })
sortedList = append(sortedList, 0)
copy(sortedList[insertIdx+1:], sortedList[insertIdx:])
sortedList[insertIdx] = newEl
ln++
// find j of nearest element that is greater or equal to 'num' in 'sortedList' using binary search
nearestNumIdx := sort.Search(ln, func(j int) bool { return sortedList[j] >= num })
// checking both 'nearestNumIdx' and 'nearestNumIdx'-1, it might be closer to num
for j := nearestNumIdx - 1; j <= nearestNumIdx; j++ {
if j < 0 || j >= ln { continue }
res = min(res, abs(num - sortedList[j]))
}
}
return res
}
// // import "github.com/emirpasic/gods/trees/redblacktree"
// func minAbsoluteDifference(nums []int, x int) int {
// rbt := redblacktree.NewWithIntComparator()
// res := 1 << 31
// for i := x; i < len(nums); i++ {
// rbt.Put(nums[i-x], nil)
// c, _ := rbt.Ceiling(nums[i])
// f, _ := rbt.Floor(nums[i])
// if c != nil {
// res = min(res, c.Key.(int)-nums[i])
// }
// if f != nil {
// res = min(res, nums[i]-f.Key.(int))
// }
// }
// return res
// }
func main() {
// Example 1:
// Input: nums = [4,3,2,4], x = 2
// Output: 0
// Explanation: We can select nums[0] = 4 and nums[3] = 4.
// They are at least 2 indices apart, and their absolute difference is the minimum, 0.
// It can be shown that 0 is the optimal answer.
fmt.Println(minAbsoluteDifference([]int{4,3,2,4}, 2)) // 0
// Example 2:
// Input: nums = [5,3,2,10,15], x = 1
// Output: 1
// Explanation: We can select nums[1] = 3 and nums[2] = 2.
// They are at least 1 index apart, and their absolute difference is the minimum, 1.
// It can be shown that 1 is the optimal answer.
fmt.Println(minAbsoluteDifference([]int{5,3,2,10,15}, 1)) // 1
// Example 3:
// Input: nums = [1,2,3,4], x = 3
// Output: 3
// Explanation: We can select nums[0] = 1 and nums[3] = 4.
// They are at least 3 indices apart, and their absolute difference is the minimum, 3.
// It can be shown that 3 is the optimal answer.
fmt.Println(minAbsoluteDifference([]int{1,2,3,4}, 3)) // 3
fmt.Println(minAbsoluteDifference([]int{1,2,3,4,5,6,7,8,9}, 2)) // 2
fmt.Println(minAbsoluteDifference([]int{9,8,7,6,5,4,3,2,1}, 2)) // 2
}