-
Notifications
You must be signed in to change notification settings - Fork 0
/
1300_find_best_value.cc
55 lines (48 loc) · 1.4 KB
/
1300_find_best_value.cc
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
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <iostream>
#include <tuple>
#include <queue>
#include <stack>
using namespace::std;
class Solution {
public:
int findBestValue(vector<int>& arr, int target) {
int n = arr.size(), result = 0, offset = INT32_MAX;
vector<int> sum(n, 0);
sort(arr.begin(), arr.end());
sum[0] = arr[0];
for (int i = 1; i < n; ++i)
sum[i] += sum[i - 1] + arr[i];
int minval = target / n, maxval = arr.back();
if (sum[n - 1] <= target)
return maxval;
while (minval <= maxval) {
int midval = (minval + maxval + 1) / 2;
auto pos = lower_bound(arr.begin(), arr.end(), midval);
int low_nums = pos - arr.begin();
int big_nums = arr.end() - pos;
int val = big_nums * midval;
if (low_nums > 0)
val += sum[low_nums - 1];
if (abs(val - target) < offset) {
offset = abs(val - target);
result = midval;
} else if (abs(val - target) == offset && midval < result) {
result = midval;
}
if (val < target)
minval = midval + 1;
else
maxval = midval - 1;
}
return result;
}
};
int main()
{
return 0;
}