|
46 | 46 |
|
47 | 47 | <!-- 这里可写通用的实现逻辑 -->
|
48 | 48 |
|
| 49 | +**方法一:排序 + 前缀和 + 二分查找 + 枚举** |
| 50 | + |
| 51 | +我们注意到,题目中要把所有大于 `value` 的值变成 `value`,并且求和,因此我们可以考虑先对数组 `arr` 进行排序,然后求出前缀和数组 $s$,其中 $s[i]$ 表示数组前 $i$ 个元素之和。 |
| 52 | + |
| 53 | +接下来,我们可以从小到大枚举所有 `value` 值,对于每个 `value`,我们可以通过二分查找找到数组中第一个大于 `value` 的元素的下标 $i$,此时数组中大于 `value` 的元素个数为 $n - i$,因此数组中小于等于 `value` 的元素个数为 $i$,此时数组中小于等于 `value` 的元素之和为 $s[i]$,数组中大于 `value` 的元素之和为 $(n - i) \times value$,因此数组中所有元素之和为 $s[i] + (n - i) \times value$。如果 $s[i] + (n - i) \times value$ 与 `target` 的差的绝对值小于当前的最小差值 `diff`,则更新 `diff` 和 `ans`。 |
| 54 | + |
| 55 | +枚举完所有 `value` 后,即可得到最终答案 `ans`。 |
| 56 | + |
| 57 | +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `arr` 的长度。 |
| 58 | + |
49 | 59 | <!-- tabs:start -->
|
50 | 60 |
|
51 | 61 | ### **Python3**
|
52 | 62 |
|
53 | 63 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
54 | 64 |
|
55 | 65 | ```python
|
56 |
| - |
| 66 | +class Solution: |
| 67 | + def findBestValue(self, arr: List[int], target: int) -> int: |
| 68 | + arr.sort() |
| 69 | + s = list(accumulate(arr, initial=0)) |
| 70 | + ans, diff = 0, inf |
| 71 | + for value in range(max(arr) + 1): |
| 72 | + i = bisect_right(arr, value) |
| 73 | + d = abs(s[i] + (len(arr) - i) * value - target) |
| 74 | + if diff > d: |
| 75 | + diff = d |
| 76 | + ans = value |
| 77 | + return ans |
57 | 78 | ```
|
58 | 79 |
|
59 | 80 | ### **Java**
|
60 | 81 |
|
61 | 82 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
62 | 83 |
|
63 | 84 | ```java
|
| 85 | +class Solution { |
| 86 | + public int findBestValue(int[] arr, int target) { |
| 87 | + Arrays.sort(arr); |
| 88 | + int n = arr.length; |
| 89 | + int[] s = new int[n + 1]; |
| 90 | + int mx = 0; |
| 91 | + for (int i = 0; i < n; ++i) { |
| 92 | + s[i + 1] = s[i] + arr[i]; |
| 93 | + mx = Math.max(mx, arr[i]); |
| 94 | + } |
| 95 | + int ans = 0, diff = 1 << 30; |
| 96 | + for (int value = 0; value <= mx; ++value) { |
| 97 | + int i = search(arr, value); |
| 98 | + int d = Math.abs(s[i] + (n - i) * value - target); |
| 99 | + if (diff > d) { |
| 100 | + diff = d; |
| 101 | + ans = value; |
| 102 | + } |
| 103 | + } |
| 104 | + return ans; |
| 105 | + } |
| 106 | + |
| 107 | + private int search(int[] arr, int x) { |
| 108 | + int left = 0, right = arr.length; |
| 109 | + while (left < right) { |
| 110 | + int mid = (left + right) >> 1; |
| 111 | + if (arr[mid] > x) { |
| 112 | + right = mid; |
| 113 | + } else { |
| 114 | + left = mid + 1; |
| 115 | + } |
| 116 | + } |
| 117 | + return left; |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### **C++** |
| 123 | + |
| 124 | +```cpp |
| 125 | +class Solution { |
| 126 | +public: |
| 127 | + int findBestValue(vector<int>& arr, int target) { |
| 128 | + sort(arr.begin(), arr.end()); |
| 129 | + int n = arr.size(); |
| 130 | + int s[n + 1]; |
| 131 | + s[0] = 0; |
| 132 | + int mx = 0; |
| 133 | + for (int i = 0; i < n; ++i) { |
| 134 | + s[i + 1] = s[i] + arr[i]; |
| 135 | + mx = max(mx, arr[i]); |
| 136 | + } |
| 137 | + int ans = 0, diff = 1 << 30; |
| 138 | + for (int value = 0; value <= mx; ++value) { |
| 139 | + int i = upper_bound(arr.begin(), arr.end(), value) - arr.begin(); |
| 140 | + int d = abs(s[i] + (n - i) * value - target); |
| 141 | + if (diff > d) { |
| 142 | + diff = d; |
| 143 | + ans = value; |
| 144 | + } |
| 145 | + } |
| 146 | + return ans; |
| 147 | + } |
| 148 | +}; |
| 149 | +``` |
64 | 150 |
|
| 151 | +### **Go** |
| 152 | +
|
| 153 | +```go |
| 154 | +func findBestValue(arr []int, target int) (ans int) { |
| 155 | + sort.Ints(arr) |
| 156 | + n := len(arr) |
| 157 | + s := make([]int, n+1) |
| 158 | + mx := 0 |
| 159 | + for i, x := range arr { |
| 160 | + s[i+1] = s[i] + x |
| 161 | + mx = max(mx, x) |
| 162 | + } |
| 163 | + diff := 1 << 30 |
| 164 | + for value := 0; value <= mx; value++ { |
| 165 | + i := sort.SearchInts(arr, value+1) |
| 166 | + d := abs(s[i] + (n-i)*value - target) |
| 167 | + if diff > d { |
| 168 | + diff = d |
| 169 | + ans = value |
| 170 | + } |
| 171 | + } |
| 172 | + return |
| 173 | +} |
| 174 | +
|
| 175 | +func abs(x int) int { |
| 176 | + if x < 0 { |
| 177 | + return -x |
| 178 | + } |
| 179 | + return x |
| 180 | +} |
| 181 | +
|
| 182 | +func max(a, b int) int { |
| 183 | + if a > b { |
| 184 | + return a |
| 185 | + } |
| 186 | + return b |
| 187 | +} |
65 | 188 | ```
|
66 | 189 |
|
67 | 190 | ### **...**
|
|
0 commit comments