Skip to content

Commit 932793c

Browse files
committed
feat: add solutions to lc problem: No.1300
No.1300.Sum of Mutated Array Closest to Target
1 parent 7865a49 commit 932793c

File tree

6 files changed

+343
-2
lines changed

6 files changed

+343
-2
lines changed

solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README.md

+124-1
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,145 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

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+
4959
<!-- tabs:start -->
5060

5161
### **Python3**
5262

5363
<!-- 这里可写当前语言的特殊实现逻辑 -->
5464

5565
```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
5778
```
5879

5980
### **Java**
6081

6182
<!-- 这里可写当前语言的特殊实现逻辑 -->
6283

6384
```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+
```
64150
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+
}
65188
```
66189

67190
### **...**

solution/1300-1399/1300.Sum of Mutated Array Closest to Target/README_EN.md

+114-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,126 @@
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def findBestValue(self, arr: List[int], target: int) -> int:
53+
arr.sort()
54+
s = list(accumulate(arr, initial=0))
55+
ans, diff = 0, inf
56+
for value in range(max(arr) + 1):
57+
i = bisect_right(arr, value)
58+
d = abs(s[i] + (len(arr) - i) * value - target)
59+
if diff > d:
60+
diff = d
61+
ans = value
62+
return ans
5263
```
5364

5465
### **Java**
5566

5667
```java
68+
class Solution {
69+
public int findBestValue(int[] arr, int target) {
70+
Arrays.sort(arr);
71+
int n = arr.length;
72+
int[] s = new int[n + 1];
73+
int mx = 0;
74+
for (int i = 0; i < n; ++i) {
75+
s[i + 1] = s[i] + arr[i];
76+
mx = Math.max(mx, arr[i]);
77+
}
78+
int ans = 0, diff = 1 << 30;
79+
for (int value = 0; value <= mx; ++value) {
80+
int i = search(arr, value);
81+
int d = Math.abs(s[i] + (n - i) * value - target);
82+
if (diff > d) {
83+
diff = d;
84+
ans = value;
85+
}
86+
}
87+
return ans;
88+
}
89+
90+
private int search(int[] arr, int x) {
91+
int left = 0, right = arr.length;
92+
while (left < right) {
93+
int mid = (left + right) >> 1;
94+
if (arr[mid] > x) {
95+
right = mid;
96+
} else {
97+
left = mid + 1;
98+
}
99+
}
100+
return left;
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int findBestValue(vector<int>& arr, int target) {
111+
sort(arr.begin(), arr.end());
112+
int n = arr.size();
113+
int s[n + 1];
114+
s[0] = 0;
115+
int mx = 0;
116+
for (int i = 0; i < n; ++i) {
117+
s[i + 1] = s[i] + arr[i];
118+
mx = max(mx, arr[i]);
119+
}
120+
int ans = 0, diff = 1 << 30;
121+
for (int value = 0; value <= mx; ++value) {
122+
int i = upper_bound(arr.begin(), arr.end(), value) - arr.begin();
123+
int d = abs(s[i] + (n - i) * value - target);
124+
if (diff > d) {
125+
diff = d;
126+
ans = value;
127+
}
128+
}
129+
return ans;
130+
}
131+
};
132+
```
57133
134+
### **Go**
135+
136+
```go
137+
func findBestValue(arr []int, target int) (ans int) {
138+
sort.Ints(arr)
139+
n := len(arr)
140+
s := make([]int, n+1)
141+
mx := 0
142+
for i, x := range arr {
143+
s[i+1] = s[i] + x
144+
mx = max(mx, x)
145+
}
146+
diff := 1 << 30
147+
for value := 0; value <= mx; value++ {
148+
i := sort.SearchInts(arr, value+1)
149+
d := abs(s[i] + (n-i)*value - target)
150+
if diff > d {
151+
diff = d
152+
ans = value
153+
}
154+
}
155+
return
156+
}
157+
158+
func abs(x int) int {
159+
if x < 0 {
160+
return -x
161+
}
162+
return x
163+
}
164+
165+
func max(a, b int) int {
166+
if a > b {
167+
return a
168+
}
169+
return b
170+
}
58171
```
59172

60173
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int findBestValue(vector<int>& arr, int target) {
4+
sort(arr.begin(), arr.end());
5+
int n = arr.size();
6+
int s[n + 1];
7+
s[0] = 0;
8+
int mx = 0;
9+
for (int i = 0; i < n; ++i) {
10+
s[i + 1] = s[i] + arr[i];
11+
mx = max(mx, arr[i]);
12+
}
13+
int ans = 0, diff = 1 << 30;
14+
for (int value = 0; value <= mx; ++value) {
15+
int i = upper_bound(arr.begin(), arr.end(), value) - arr.begin();
16+
int d = abs(s[i] + (n - i) * value - target);
17+
if (diff > d) {
18+
diff = d;
19+
ans = value;
20+
}
21+
}
22+
return ans;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
func findBestValue(arr []int, target int) (ans int) {
2+
sort.Ints(arr)
3+
n := len(arr)
4+
s := make([]int, n+1)
5+
mx := 0
6+
for i, x := range arr {
7+
s[i+1] = s[i] + x
8+
mx = max(mx, x)
9+
}
10+
diff := 1 << 30
11+
for value := 0; value <= mx; value++ {
12+
i := sort.SearchInts(arr, value+1)
13+
d := abs(s[i] + (n-i)*value - target)
14+
if diff > d {
15+
diff = d
16+
ans = value
17+
}
18+
}
19+
return
20+
}
21+
22+
func abs(x int) int {
23+
if x < 0 {
24+
return -x
25+
}
26+
return x
27+
}
28+
29+
func max(a, b int) int {
30+
if a > b {
31+
return a
32+
}
33+
return b
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int findBestValue(int[] arr, int target) {
3+
Arrays.sort(arr);
4+
int n = arr.length;
5+
int[] s = new int[n + 1];
6+
int mx = 0;
7+
for (int i = 0; i < n; ++i) {
8+
s[i + 1] = s[i] + arr[i];
9+
mx = Math.max(mx, arr[i]);
10+
}
11+
int ans = 0, diff = 1 << 30;
12+
for (int value = 0; value <= mx; ++value) {
13+
int i = search(arr, value);
14+
int d = Math.abs(s[i] + (n - i) * value - target);
15+
if (diff > d) {
16+
diff = d;
17+
ans = value;
18+
}
19+
}
20+
return ans;
21+
}
22+
23+
private int search(int[] arr, int x) {
24+
int left = 0, right = arr.length;
25+
while (left < right) {
26+
int mid = (left + right) >> 1;
27+
if (arr[mid] > x) {
28+
right = mid;
29+
} else {
30+
left = mid + 1;
31+
}
32+
}
33+
return left;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def findBestValue(self, arr: List[int], target: int) -> int:
3+
arr.sort()
4+
s = list(accumulate(arr, initial=0))
5+
ans, diff = 0, inf
6+
for value in range(max(arr) + 1):
7+
i = bisect_right(arr, value)
8+
d = abs(s[i] + (len(arr) - i) * value - target)
9+
if diff > d:
10+
diff = d
11+
ans = value
12+
return ans

0 commit comments

Comments
 (0)