-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2602-MinimumOperationsToMakeAllArrayElementsEqual.go
More file actions
105 lines (93 loc) · 4.06 KB
/
2602-MinimumOperationsToMakeAllArrayElementsEqual.go
File metadata and controls
105 lines (93 loc) · 4.06 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
package main
// 2602. Minimum Operations to Make All Array Elements Equal
// You are given an array nums consisting of positive integers.
// You are also given an integer array queries of size m.
// For the ith query, you want to make all of the elements of nums equal to queries[i].
// You can perform the following operation on the array any number of times:
// Increase or decrease an element of the array by 1.
// Return an array answer of size m where answer[i] is the minimum number of operations to make all elements of nums equal to queries[i].
// Note that after each query the array is reset to its original state.
// Example 1:
// Input: nums = [3,1,6,8], queries = [1,5]
// Output: [14,10]
// Explanation: For the first query we can do the following operations:
// - Decrease nums[0] 2 times, so that nums = [1,1,6,8].
// - Decrease nums[2] 5 times, so that nums = [1,1,1,8].
// - Decrease nums[3] 7 times, so that nums = [1,1,1,1].
// So the total number of operations for the first query is 2 + 5 + 7 = 14.
// For the second query we can do the following operations:
// - Increase nums[0] 2 times, so that nums = [5,1,6,8].
// - Increase nums[1] 4 times, so that nums = [5,5,6,8].
// - Decrease nums[2] 1 time, so that nums = [5,5,5,8].
// - Decrease nums[3] 3 times, so that nums = [5,5,5,5].
// So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.
// Example 2:
// Input: nums = [2,9,6,3], queries = [10]
// Output: [20]
// Explanation: We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.
// Constraints:
// n == nums.length
// m == queries.length
// 1 <= n, m <= 10^5
// 1 <= nums[i], queries[i] <= 10^9
import "fmt"
import "sort"
func minOperations(nums []int, queries []int) []int64 {
n := len(nums)
sort.Ints(nums)
prefix := make([]int, n)
prefix[0] = nums[0]
for i := 1; i < n; i++ {
prefix[i] = prefix[i-1] + nums[i]
}
res := make([]int64, len(queries))
for i, query := range queries {
index := sort.SearchInts(nums, query)
if index == 0 {
res[i] = int64(prefix[n - 1] - query * n)
} else {
res[i] = int64(prefix[n - 1] - query * (n - index) + query * index - prefix[index - 1] << 1)
}
}
return res
}
func minOperations1(nums []int, queries []int) []int64 {
n := len(nums)
// slices.Sort(nums)
sort.Ints(nums)
prefix := make([]int, n + 1)
for i := range nums {
prefix[i + 1] = prefix[i] + nums[i]
}
res := make([]int64, len(queries))
for i, q := range queries {
// j, _ := slices.BinarySearch(nums, q)
j := sort.SearchInts(nums, q)
res[i] = int64((q * j - prefix[j]) + (prefix[n] - prefix[j] - q * (n - j)))
}
return res
}
func main() {
// Example 1:
// Input: nums = [3,1,6,8], queries = [1,5]
// Output: [14,10]
// Explanation: For the first query we can do the following operations:
// - Decrease nums[0] 2 times, so that nums = [1,1,6,8].
// - Decrease nums[2] 5 times, so that nums = [1,1,1,8].
// - Decrease nums[3] 7 times, so that nums = [1,1,1,1].
// So the total number of operations for the first query is 2 + 5 + 7 = 14.
// For the second query we can do the following operations:
// - Increase nums[0] 2 times, so that nums = [5,1,6,8].
// - Increase nums[1] 4 times, so that nums = [5,5,6,8].
// - Decrease nums[2] 1 time, so that nums = [5,5,5,8].
// - Decrease nums[3] 3 times, so that nums = [5,5,5,5].
// So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.
fmt.Println(minOperations([]int{3,1,6,8}, []int{1,5})) // [14,10]
// Example 2:
// Input: nums = [2,9,6,3], queries = [10]
// Output: [20]
// Explanation: We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.
fmt.Println(minOperations([]int{2,9,6,3}, []int{10})) // [20]
fmt.Println(minOperations1([]int{3,1,6,8}, []int{1,5})) // [14,10]
fmt.Println(minOperations1([]int{2,9,6,3}, []int{10})) // [20]
}