|
47 | 47 |
|
48 | 48 | ## Solutions
|
49 | 49 |
|
| 50 | +**Solution 1: Hash Table + Enumeration** |
| 51 | + |
| 52 | +According to the problem description, we know that the function $func(arr, l, r)$ is actually the bitwise AND result of the elements in the array $arr$ from index $l$ to $r$, i.e., $arr[l] \& arr[l + 1] \& \cdots \& arr[r]$. |
| 53 | + |
| 54 | +If we fix the right endpoint $r$ each time, then the range of the left endpoint $l$ is $[0, r]$. Since the sum of bitwise ANDs decreases monotonically with decreasing $l$, and the value of $arr[i]$ does not exceed $10^6$, there are at most $20$ different values in the interval $[0, r]$. Therefore, we can use a set to maintain all the values of $arr[l] \& arr[l + 1] \& \cdots \& arr[r]$. When we traverse from $r$ to $r+1$, the value with $r+1$ as the right endpoint is the value obtained by performing bitwise AND with each value in the set and $arr[r + 1]$, plus $arr[r + 1]$ itself. Therefore, we only need to enumerate each value in the set, perform bitwise AND with $arr[r]$, to obtain all the values with $r$ as the right endpoint. Then we subtract each value from $target$ and take the absolute value to obtain the absolute difference between each value and $target$. The minimum value among them is the answer. |
| 55 | + |
| 56 | +The time complexity is $O(n \times \log M)$, and the space complexity is $O(\log M)$. Here, $n$ and $M$ are the length of the array $arr$ and the maximum value in the array $arr$, respectively. |
| 57 | + |
50 | 58 | <!-- tabs:start -->
|
51 | 59 |
|
52 | 60 | ### **Python3**
|
53 | 61 |
|
54 | 62 | ```python
|
55 |
| - |
| 63 | +class Solution: |
| 64 | + def closestToTarget(self, arr: List[int], target: int) -> int: |
| 65 | + ans = abs(arr[0] - target) |
| 66 | + s = {arr[0]} |
| 67 | + for x in arr: |
| 68 | + s = {x & y for y in s} | {x} |
| 69 | + ans = min(ans, min(abs(y - target) for y in s)) |
| 70 | + return ans |
56 | 71 | ```
|
57 | 72 |
|
58 | 73 | ### **Java**
|
59 | 74 |
|
60 | 75 | ```java
|
| 76 | +class Solution { |
| 77 | + public int closestToTarget(int[] arr, int target) { |
| 78 | + int ans = Math.abs(arr[0] - target); |
| 79 | + Set<Integer> pre = new HashSet<>(); |
| 80 | + pre.add(arr[0]); |
| 81 | + for (int x : arr) { |
| 82 | + Set<Integer> cur = new HashSet<>(); |
| 83 | + for (int y : pre) { |
| 84 | + cur.add(x & y); |
| 85 | + } |
| 86 | + cur.add(x); |
| 87 | + for (int y : cur) { |
| 88 | + ans = Math.min(ans, Math.abs(y - target)); |
| 89 | + } |
| 90 | + pre = cur; |
| 91 | + } |
| 92 | + return ans; |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### **C++** |
| 98 | + |
| 99 | +```cpp |
| 100 | +class Solution { |
| 101 | +public: |
| 102 | + int closestToTarget(vector<int>& arr, int target) { |
| 103 | + int ans = abs(arr[0] - target); |
| 104 | + unordered_set<int> pre; |
| 105 | + pre.insert(arr[0]); |
| 106 | + for (int x : arr) { |
| 107 | + unordered_set<int> cur; |
| 108 | + cur.insert(x); |
| 109 | + for (int y : pre) { |
| 110 | + cur.insert(x & y); |
| 111 | + } |
| 112 | + for (int y : cur) { |
| 113 | + ans = min(ans, abs(y - target)); |
| 114 | + } |
| 115 | + pre = move(cur); |
| 116 | + } |
| 117 | + return ans; |
| 118 | + } |
| 119 | +}; |
| 120 | +``` |
| 121 | +
|
| 122 | +### **Go** |
| 123 | +
|
| 124 | +```go |
| 125 | +func closestToTarget(arr []int, target int) int { |
| 126 | + ans := abs(arr[0] - target) |
| 127 | + pre := map[int]bool{arr[0]: true} |
| 128 | + for _, x := range arr { |
| 129 | + cur := map[int]bool{x: true} |
| 130 | + for y := range pre { |
| 131 | + cur[x&y] = true |
| 132 | + } |
| 133 | + for y := range cur { |
| 134 | + ans = min(ans, abs(y-target)) |
| 135 | + } |
| 136 | + pre = cur |
| 137 | + } |
| 138 | + return ans |
| 139 | +} |
| 140 | +
|
| 141 | +func min(a, b int) int { |
| 142 | + if a < b { |
| 143 | + return a |
| 144 | + } |
| 145 | + return b |
| 146 | +} |
| 147 | +
|
| 148 | +func abs(x int) int { |
| 149 | + if x < 0 { |
| 150 | + return -x |
| 151 | + } |
| 152 | + return x |
| 153 | +} |
| 154 | +``` |
61 | 155 |
|
| 156 | +### **TypeScript** |
| 157 | + |
| 158 | +```ts |
| 159 | +function closestToTarget(arr: number[], target: number): number { |
| 160 | + let ans = Math.abs(arr[0] - target); |
| 161 | + let pre = new Set<number>(); |
| 162 | + pre.add(arr[0]); |
| 163 | + for (const x of arr) { |
| 164 | + const cur = new Set<number>(); |
| 165 | + cur.add(x); |
| 166 | + for (const y of pre) { |
| 167 | + cur.add(x & y); |
| 168 | + } |
| 169 | + for (const y of cur) { |
| 170 | + ans = Math.min(ans, Math.abs(y - target)); |
| 171 | + } |
| 172 | + pre = cur; |
| 173 | + } |
| 174 | + return ans; |
| 175 | +} |
62 | 176 | ```
|
63 | 177 |
|
64 | 178 | ### **...**
|
|
0 commit comments