You are given an array points of size n and an integer m. There is another array gameScore of size n, where gameScore[i] represents the score achieved at the ith game. Initially, gameScore[i] == 0 for all i.
You start at index -1, which is outside the array (before the first position at index 0). You can make at mostm moves. In each move, you can either:
Increase the index by 1 and add points[i] to gameScore[i].
Decrease the index by 1 and add points[i] to gameScore[i].
Note that the index must always remain within the bounds of the array after the first move.
Return the maximum possible minimum value in gameScore after at mostm moves.
Input: points =[2,4], m =3Output: 4Explanation:
Initially, index `i = -1` and `gameScore = [0, 0]`.Move | Index | gameScore
---|---|---Increase `i`|0|`[2, 0]`Increase `i`|1|`[2, 4]`Decrease `i`|0|`[4, 4]`The minimum value in`gameScore`is4, and thisis the maximum possible
minimum among all configurations. Hence,4is the output.
Input: points =[1,2,3], m =5Output: 2Explanation:
Initially, index `i = -1` and `gameScore = [0, 0, 0]`.Move | Index | gameScore
---|---|---Increase `i`|0|`[1, 0, 0]`Increase `i`|1|`[1, 2, 0]`Decrease `i`|0|`[2, 2, 0]`Increase `i`|1|`[2, 4, 0]`Increase `i`|2|`[2, 4, 3]`The minimum value in`gameScore`is2, and thisis the maximum possible
minimum among all configurations. Hence,2is the output.
To maximize the minimum value in gameScore, we can use binary search on the answer. For each candidate minimum, we check if it’s possible to distribute at most m moves so that every gameScore[i] is at least that value, by greedily allocating moves to the most efficient positions.
classSolution {
public:int maximizeMinScore(vector<int>& points, int m) {
int n = points.size();
int l =0, r =1e15, ans =0;
while (l <= r) {
longlong mid = l + (r - l) /2, need =0;
for (int i =0; i < n; ++i) {
need += (mid + points[i] -1) / points[i];
}
if (need <= m) { ans = mid; l = mid +1; }
else r = mid -1;
}
return ans;
}
};
classSolution {
publicintmaximizeMinScore(int[] points, int m) {
int n = points.length;
long l = 0, r = (long)1e15, ans = 0;
while (l <= r) {
long mid = (l + r) / 2, need = 0;
for (int i = 0; i < n; i++) {
need += (mid + points[i]- 1) / points[i];
}
if (need <= m) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return (int)ans;
}
}
classSolution {
funmaximizeMinScore(points: IntArray, m: Int): Int {
var l = 0Lvar r = 1_000_000_000_000_000L
var ans = 0Lwhile (l <= r) {
val mid = (l + r) / 2var need = 0Lfor (p in points) {
need += (mid + p - 1) / p
}
if (need <= m) {
ans = mid
l = mid + 1 } else {
r = mid - 1 }
}
return ans.toInt()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defmaximizeMinScore(self, points: list[int], m: int) -> int:
defenough(x: int) -> bool:
return sum((x + p -1) // p for p in points) <= m
l, r, ans =0, 10**15, 0while l <= r:
mid = (l + r) //2if enough(mid):
ans = mid
l = mid +1else:
r = mid -1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfnmaximize_min_score(points: Vec<i64>, m: i64) -> i64 {
let (mut l, mut r, mut ans) = (0, 1_000_000_000_000_000, 0);
while l <= r {
let mid = (l + r) /2;
let need: i64= points.iter().map(|&p| (mid + p -1) / p).sum();
if need <= m {
ans = mid;
l = mid +1;
} else {
r = mid -1;
}
}
ans
}
}