-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3766-MinimumOperationsToMakeBinaryPalindrome.go
More file actions
135 lines (122 loc) · 4.96 KB
/
3766-MinimumOperationsToMakeBinaryPalindrome.go
File metadata and controls
135 lines (122 loc) · 4.96 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
package main
// 3766. Minimum Operations to Make Binary Palindrome
// You are given an integer array nums.
// For each element nums[i], you may perform the following operations any number of times (including zero):
// 1. Increase nums[i] by 1, or
// 2. Decrease nums[i] by 1.
// A number is called a binary palindrome if its binary representation without leading zeros reads the same forward and backward.
// Your task is to return an integer array ans, where ans[i] represents the minimum number of operations required to convert nums[i] into a binary palindrome.
// Example 1:
// Input: nums = [1,2,4]
// Output: [0,1,1]
// Explanation:
// One optimal set of operations:
// nums[i] | Binary(nums[i]) | Nearest Palindrome | Binary (Palindrome) | Operations Required | ans[i]
// 1 | 1 | 1 | 1 | Already palindrome | 0
// 2 | 10 | 3 | 11 | Increase by 1 | 1
// 4 | 100 | 3 | 11 | Decrease by 1 | 1
// Thus, ans = [0, 1, 1].
// Example 2:
// Input: nums = [6,7,12]
// Output: [1,0,3]
// Explanation:
// One optimal set of operations:
// nums[i] | Binary(nums[i]) | Nearest Palindrome | Binary (Palindrome) | Operations Required | ans[i]
// 6 | 110 | 5 | 101 | Decrease by 1 | 1
// 7 | 111 | 7 | 111 | Already palindrome | 0
// 12 | 1100 | 15 | 1111 | Increase by 3 | 3
// Thus, ans = [1, 0, 3].
// Constraints:
// 1 <= nums.length <= 5000
// 1 <= nums[i] <= 5000
import "fmt"
import "math/bits"
// Brute Force
func minOperations(nums []int) []int {
res := make([]int, len(nums))
isPalindrome := func(x int) bool {
arr := []int{}
for x > 0 {
arr = append(arr, x%2)
x /= 2
}
i, j := 0, len(arr) - 1
for i <= j {
if arr[i] != arr[j] {
return false
}
i++
j--
}
return true
}
getResult :=func (x int) int {
res, delta,front,back := 1 << 31, 0, false, false
for !front && !back {
f, b := x + delta, x - delta
if isPalindrome(f) {
res = min(res, (f - x))
front = true
}
if isPalindrome(b) {
res = min(res, (x - b))
back = true
}
delta++
}
return res
}
for i := 0; i < len(nums); i++ {
res[i] = getResult(nums[i])
}
return res
}
// 位运算
func minOperations1(nums []int) []int {
abs := func(x int) int { if x < 0 { return -x }; return x }
for i, x := range nums {
res := 1 << 31
n := bits.Len(uint(x))
m := n / 2
left := x >> m
for l := left - 1; l <= left+1; l++ {
// 左半反转到右半
// 如果 n 是奇数,那么去掉回文中心再反转
right := bits.Reverse(uint(l>>(n%2))) >> (bits.UintSize - m)
pal := l << m | int(right)
res = min(res, abs(x-pal))
}
nums[i] = res
}
return nums
}
func main() {
// Example 1:
// Input: nums = [1,2,4]
// Output: [0,1,1]
// Explanation:
// One optimal set of operations:
// nums[i] | Binary(nums[i]) | Nearest Palindrome | Binary (Palindrome) | Operations Required | ans[i]
// 1 | 1 | 1 | 1 | Already palindrome | 0
// 2 | 10 | 3 | 11 | Increase by 1 | 1
// 4 | 100 | 3 | 11 | Decrease by 1 | 1
// Thus, ans = [0, 1, 1].
fmt.Println(minOperations([]int{1,2,4})) // [0, 1, 1]
// Example 2:
// Input: nums = [6,7,12]
// Output: [1,0,3]
// Explanation:
// One optimal set of operations:
// nums[i] | Binary(nums[i]) | Nearest Palindrome | Binary (Palindrome) | Operations Required | ans[i]
// 6 | 110 | 5 | 101 | Decrease by 1 | 1
// 7 | 111 | 7 | 111 | Already palindrome | 0
// 12 | 1100 | 15 | 1111 | Increase by 3 | 3
// Thus, ans = [1, 0, 3].
fmt.Println(minOperations([]int{6,7,12})) // [1, 0, 3]
fmt.Println(minOperations([]int{1,2,3,4,5,6,7,8,9})) // [0 1 0 1 0 1 0 1 0]
fmt.Println(minOperations([]int{9,8,7,6,5,4,3,2,1})) // [0 1 0 1 0 1 0 1 0]
fmt.Println(minOperations1([]int{1,2,4})) // [0, 1, 1]
fmt.Println(minOperations1([]int{6,7,12})) // [1, 0, 3]
fmt.Println(minOperations1([]int{1,2,3,4,5,6,7,8,9})) // [0 1 0 1 0 1 0 1 0]
fmt.Println(minOperations1([]int{9,8,7,6,5,4,3,2,1})) // [0 1 0 1 0 1 0 1 0]
}