-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3171-FindSubarrayWithBitwiseORClosestToK.go
More file actions
172 lines (157 loc) · 5.74 KB
/
3171-FindSubarrayWithBitwiseORClosestToK.go
File metadata and controls
172 lines (157 loc) · 5.74 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
// 3171. Find Subarray With Bitwise OR Closest to K
// You are given an array nums and an integer k.
// You need to find a subarray of nums such that the absolute difference between k and the bitwise OR of the subarray elements is as small as possible.
// In other words, select a subarray nums[l..r] such that |k - (nums[l] OR nums[l + 1] ... OR nums[r])| is minimum.
// Return the minimum possible value of the absolute difference.
// A subarray is a contiguous non-empty sequence of elements within an array.
// Example 1:
// Input: nums = [1,2,4,5], k = 3
// Output: 0
// Explanation:
// The subarray nums[0..1] has OR value 3, which gives the minimum absolute difference |3 - 3| = 0.
// Example 2:
// Input: nums = [1,3,1,3], k = 2
// Output: 1
// Explanation:
// The subarray nums[1..1] has OR value 3, which gives the minimum absolute difference |3 - 2| = 1.
// Example 3:
// Input: nums = [1], k = 10
// Output: 9
// Explanation:
// There is a single subarray with OR value 1, which gives the minimum absolute difference |10 - 1| = 9.
// Constraints:
// 1 <= nums.length <= 10^5
// 1 <= nums[i] <= 10^9
// 1 <= k <= 10^9
import "fmt"
import "sort"
// func minimumDifference(nums []int, k int) int {
// res, n := 1 << 31, len(nums)
// 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 := 0; i < n; i++ {
// for j := i; j >= 0; j-- {
// val := nums[i] & nums[j]
// res = min(res, abs(k - val))
// if i != j && val == nums[j] { break }
// nums[j] = val
// }
// }
// return res
// }
// func minimumDifference(nums []int, k int) int {
// res, dp, n := 1 << 31, make([]int,100001), len(nums)
// 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 := 0; i < n; i++ {
// for j := i; j < n; j++ {
// val := nums[i]
// val &= nums[j]
// res = min(res, abs(k - val))
// if val <= k || val == dp[j] { break }
// dp[j] = val
// }
// }
// return res
// }
// func minimumDifference(nums []int, k int) int {
// res := 1 << 31
// subArrayANDValues := make(map[int]bool)
// 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 _, v := range nums {
// newSubArrayANDValues := make(map[int]bool)
// newSubArrayANDValues[v] = true
// res = min(res, abs(v - k))
// for v1, _ := range subArrayANDValues {
// val := v1 & v
// if _, ok := newSubArrayANDValues[val]; !ok {
// newSubArrayANDValues[val] = true
// res = min(res, abs(val - k))
// }
// }
// subArrayANDValues = newSubArrayANDValues
// }
// return res
// }
func minimumDifference(nums []int, k int) int {
res, n, bitsMaxPos := 1 << 31, len(nums), make([]int, 31)
for i := range bitsMaxPos {
bitsMaxPos[i] = -1
}
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 := 0; i < n; i++ {
for j := 0; j <= 30; j++ {
if nums[i]>>j & 1 == 1 {
bitsMaxPos[j] = i
}
}
posToBit := make([][2]int, 0)
for j := 0; j <= 30; j++ {
if bitsMaxPos[j] != -1 {
posToBit = append(posToBit, [2]int{bitsMaxPos[j], j})
}
}
sort.Slice(posToBit, func(a, b int) bool {
return posToBit[a][0] > posToBit[b][0]
})
val := 0
for j, p := 0, 0; j < len(posToBit); p = j {
for j < len(posToBit) && posToBit[j][0] == posToBit[p][0] {
val |= 1 << posToBit[j][1]
j++
}
res = min(res, abs(val - k))
}
}
return res
}
func minimumDifference1(nums []int, k int) int {
res, left, bottom, rightOr := 1 << 31, 0, 0, 0
min := func (x, y int) int { if x < y { return x; }; return y; }
for right, v := range nums {
rightOr |= v
for left <= right && nums[left] | rightOr > k {
res = min(res, (nums[left] | rightOr) - k)
if bottom <= left {
// 重新构建一个栈
// 由于 left 即将移出窗口,只需计算到 left+1
for i := right - 1; i > left; i-- {
nums[i] |= nums[i+1]
}
bottom = right
rightOr = 0
}
left++
}
if left <= right {
res = min(res, k - (nums[left] | rightOr))
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [1,2,4,5], k = 3
// Output: 0
// Explanation:
// The subarray nums[0..1] has OR value 3, which gives the minimum absolute difference |3 - 3| = 0.
fmt.Println(minimumDifference([]int{1,2,4,5}, 3)) // 0
// Example 2:
// Input: nums = [1,3,1,3], k = 2
// Output: 1
// Explanation:
// The subarray nums[1..1] has OR value 3, which gives the minimum absolute difference |3 - 2| = 1.
fmt.Println(minimumDifference([]int{1,3,1,3}, 2)) // 1
// Example 3:
// Input: nums = [1], k = 10
// Output: 9
// Explanation:
// There is a single subarray with OR value 1, which gives the minimum absolute difference |10 - 1| = 9.
fmt.Println(minimumDifference([]int{1}, 10)) // 9
fmt.Println(minimumDifference1([]int{1,2,4,5}, 3)) // 0
fmt.Println(minimumDifference1([]int{1,3,1,3}, 2)) // 1
fmt.Println(minimumDifference1([]int{1}, 10)) // 9
}