Input: tiles =[[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen =10Output: 9Explanation: Place the carpet starting on tile 10.It covers 9 white tiles, so we return9.Note that there may be other places where the carpet covers 9 white tiles.It can be shown that the carpet cannot cover more than 9 white tiles.
To maximize the number of white tiles covered, sort the tiles and use a sliding window to try placing the carpet starting at each tile. For each start, use binary search to find the last tile the carpet can reach, and calculate the total coverage.
classSolution {
public:int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen) {
sort(tiles.begin(), tiles.end());
int n = tiles.size(), ans =0;
vector<int> prefix(n+1);
for (int i =0; i < n; ++i)
prefix[i+1] = prefix[i] + tiles[i][1] - tiles[i][0] +1;
for (int i =0; i < n; ++i) {
int end = tiles[i][0] + carpetLen -1;
int l = i, r = n-1, j = i;
while (l <= r) {
int m = (l + r) /2;
if (tiles[m][0] <= end) { j = m; l = m +1; }
else r = m -1;
}
int covered = prefix[j] - prefix[i];
int last = min(end, tiles[j][1]);
covered += max(0, last - tiles[j][0] +1);
ans = max(ans, covered);
}
return ans;
}
};
classSolution {
publicintmaximumWhiteTiles(int[][] tiles, int carpetLen) {
Arrays.sort(tiles, (a, b) -> a[0]- b[0]);
int n = tiles.length, ans = 0;
int[] prefix =newint[n+1];
for (int i = 0; i < n; ++i)
prefix[i+1]= prefix[i]+ tiles[i][1]- tiles[i][0]+ 1;
for (int i = 0; i < n; ++i) {
int end = tiles[i][0]+ carpetLen - 1;
int l = i, r = n-1, j = i;
while (l <= r) {
int m = (l + r) / 2;
if (tiles[m][0]<= end) { j = m; l = m + 1; }
else r = m - 1;
}
int covered = prefix[j]- prefix[i];
int last = Math.min(end, tiles[j][1]);
covered += Math.max(0, last - tiles[j][0]+ 1);
ans = Math.max(ans, covered);
}
return ans;
}
}
classSolution {
funmaximumWhiteTiles(tiles: Array<IntArray>, carpetLen: Int): Int {
tiles.sortBy { it[0] }
val n = tiles.size
val prefix = IntArray(n+1)
for (i in0 until n) prefix[i+1] = prefix[i] + tiles[i][1] - tiles[i][0] + 1var ans = 0for (i in0 until n) {
val end = tiles[i][0] + carpetLen - 1var l = i; var r = n-1; var j = i
while (l <= r) {
val m = (l + r) / 2if (tiles[m][0] <= end) { j = m; l = m + 1 } else r = m - 1 }
var covered = prefix[j] - prefix[i]
val last = minOf(end, tiles[j][1])
covered += maxOf(0, last - tiles[j][0] + 1)
ans = maxOf(ans, covered)
}
return ans
}
}
defmaximum_white_tiles(tiles: list[list[int]], carpet_len: int) -> int:
tiles.sort()
n = len(tiles)
prefix = [0] * (n+1)
for i in range(n):
prefix[i+1] = prefix[i] + tiles[i][1] - tiles[i][0] +1 ans =0for i in range(n):
end = tiles[i][0] + carpet_len -1 l, r, j = i, n-1, i
while l <= r:
m = (l + r) //2if tiles[m][0] <= end:
j = m
l = m +1else:
r = m -1 covered = prefix[j] - prefix[i]
last = min(end, tiles[j][1])
covered += max(0, last - tiles[j][0] +1)
ans = max(ans, covered)
return ans
impl Solution {
pubfnmaximum_white_tiles(tiles: Vec<Vec<i32>>, carpet_len: i32) -> i32 {
letmut tiles = tiles;
tiles.sort_by_key(|x| x[0]);
let n = tiles.len();
letmut prefix =vec![0; n+1];
for i in0..n {
prefix[i+1] = prefix[i] + tiles[i][1] - tiles[i][0] +1;
}
letmut ans =0;
for i in0..n {
let end = tiles[i][0] + carpet_len -1;
let (mut l, mut r, mut j) = (i, n-1, i);
while l <= r {
let m = (l + r) /2;
if tiles[m][0] <= end { j = m; l = m +1; } else { r = m -1; }
}
letmut covered = prefix[j] - prefix[i];
let last = end.min(tiles[j][1]);
covered += (last - tiles[j][0] +1).max(0);
ans = ans.max(covered);
}
ans
}
}