[LeetCode] Maximize Score of Numbers in Ranges

3281. Maximize Score of Numbers in Ranges

You are given an array of integers start and an integer d, representing n intervals [start[i], start[i] + d].

You are asked to choose n integers where the ith integer must belong to the ith interval. The score of the chosen integers is defined as the minimum absolute difference between any two integers that have been chosen.

Return the maximum possible score of the chosen integers.

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

class Solution {
bool helper(vector<int>& A, int d, long long m) {
long long nxt = A[0] + m;
for(int i = 1; i < A.size(); i++) {
long long mi = A[i], ma = mi + d;
if(nxt > ma) return false;
nxt = max(nxt, mi) + m;
}
return true;
}
public:
int maxPossibleScore(vector<int>& start, int d) {
long long l = 0, r = INT_MAX, res = 0;
sort(begin(start), end(start));
while(l <= r) {
long long m = l + (r - l) / 2;
bool ok = helper(start,d,m);
if(ok) {
l = m + 1;
res = m;
} else r = m - 1;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/09/08/PS/LeetCode/maximize-score-of-numbers-in-ranges/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.