-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2967-MinimumCostToMakeArrayEqualindromic.go
More file actions
158 lines (140 loc) · 6.36 KB
/
2967-MinimumCostToMakeArrayEqualindromic.go
File metadata and controls
158 lines (140 loc) · 6.36 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
// 2967. Minimum Cost to Make Array Equalindromic
// You are given a 0-indexed integer array nums having length n.
// You are allowed to perform a special move any number of times (including zero) on nums.
// In one special move you perform the following steps in order:
// 1. Choose an index i in the range [0, n - 1], and a positive integer x.
// 2. Add |nums[i] - x| to the total cost.
// 3. Change the value of nums[i] to x.
// A palindromic number is a positive integer that remains the same when its digits are reversed.
// For example, 121, 2552 and 65756 are palindromic numbers whereas 24, 46, 235 are not palindromic numbers.
// An array is considered equalindromic if all the elements in the array are equal to an integer y, where y is a palindromic number less than 109.
// Return an integer denoting the minimum possible total cost to make nums equalindromic by performing any number of special moves.
// Example 1:
// Input: nums = [1,2,3,4,5]
// Output: 6
// Explanation: We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6.
// It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost.
// Example 2:
// Input: nums = [10,12,13,14,15]
// Output: 11
// Explanation: We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11.
// It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost.
// Example 3:
// Input: nums = [22,33,22,33,22]
// Output: 22
// Explanation: We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22.
// It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost.
// Constraints:
// 1 <= n <= 10^5
// 1 <= nums[i] <= 10^9
import "fmt"
import "sort"
import "strconv"
func minimumCost(nums []int) int64 {
sort.Ints(nums)
a, b, n := 0, 0, len(nums)
mid := n / 2
if n % 2 == 0 {
mid = (nums[mid - 1] + nums[mid]) / 2
} else {
mid = nums[mid]
}
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; }
isPalindromic := func(num int) bool {
str := strconv.Itoa(num)
for i, j := 0, len(str) - 1; i < j; i, j = i + 1, j - 1 {
if str[i] != str[j] { return false }
}
return true
}
i, j, low, high := mid, mid, -1, -1
for low == -1 || high == -1 {
if low == -1 && isPalindromic(i) {
low = i
}
if high == -1 && isPalindromic(j) {
high = j
}
i--
j++
}
for i := range nums {
if nums[i] != low {
a = a + abs(nums[i] - low)
}
if nums[i] != high {
b = b + abs(nums[i] - high)
}
}
return int64(min(a, b))
}
var ps [2 * 100000]int64
func init() {
reverseString := func(s string) string {
arr := []byte(s)
for i, j := 0, len(arr) - 1; i < j; i, j = i+1, j-1 {
arr[i], arr[j] = arr[j], arr[i]
}
return string(arr)
}
for i := 1; i <= 100000; i++ {
s := strconv.Itoa(i)
t1 := reverseString(s)
t2 := reverseString(s[:len(s)-1])
ps[2*i-2], _ = strconv.ParseInt(s+t1, 10, 64)
ps[2*i-1], _ = strconv.ParseInt(s+t2, 10, 64)
}
sort.Slice(ps[:], func(i, j int) bool {
return ps[i] < ps[j]
})
}
func minimumCost1(nums []int) int64 {
sort.Ints(nums)
i := sort.Search(len(ps), func(i int) bool {
return ps[i] >= int64(nums[len(nums) / 2])
})
abs := func(x int) int { if x < 0 { return -x; }; return x; }
helper := func(x int64) int64 {
res := int64(0)
for _, v := range nums {
res += int64(abs(int(x - int64(v))))
}
return res
}
res := int64(1 << 61)
for j := i - 1; j <= i + 1; j++ {
if 0 <= j && j < len(ps) {
res = min(res, helper(ps[j]))
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [1,2,3,4,5]
// Output: 6
// Explanation: We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6.
// It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost.
fmt.Println(minimumCost([]int{1,2,3,4,5})) // 6
// Example 2:
// Input: nums = [10,12,13,14,15]
// Output: 11
// Explanation: We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11.
// It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost.
fmt.Println(minimumCost([]int{10,12,13,14,15})) // 11
// Example 3:
// Input: nums = [22,33,22,33,22]
// Output: 22
// Explanation: We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22.
// It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost.
fmt.Println(minimumCost([]int{22,33,22,33,22})) // 22
fmt.Println(minimumCost([]int{1,2,3,4,5,6,7,8,9})) // 20
fmt.Println(minimumCost([]int{9,8,7,6,5,4,3,2,1})) // 20
fmt.Println(minimumCost1([]int{1,2,3,4,5})) // 6
fmt.Println(minimumCost1([]int{10,12,13,14,15})) // 11
fmt.Println(minimumCost1([]int{22,33,22,33,22})) // 22
fmt.Println(minimumCost1([]int{1,2,3,4,5,6,7,8,9})) // 20
fmt.Println(minimumCost1([]int{9,8,7,6,5,4,3,2,1})) // 20
}