2271. Maximum White Tiles Covered by a Carpet LeetCode Solution
In this guide, you will get 2271. Maximum White Tiles Covered by a Carpet LeetCode Solution with the best time and space complexity. The solution to Maximum White Tiles Covered by a Carpet problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum White Tiles Covered by a Carpet solution in C++
- Maximum White Tiles Covered by a Carpet solution in Java
- Maximum White Tiles Covered by a Carpet solution in Python
- Additional Resources
Problem Statement of Maximum White Tiles Covered by a Carpet
You are given a 2D integer array tiles where tiles[i] = [li, ri] represents that every tile j in the range li <= j <= ri is colored white.
You are also given an integer carpetLen, the length of a single carpet that can be placed anywhere.
Return the maximum number of white tiles that can be covered by the carpet.
Example 1:
Input: tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
Output: 9
Explanation: Place the carpet starting on tile 10.
It covers 9 white tiles, so we return 9.
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.
Example 2:
Input: tiles = [[10,11],[1,1]], carpetLen = 2
Output: 2
Explanation: Place the carpet starting on tile 10.
It covers 2 white tiles, so we return 2.
Constraints:
1 <= tiles.length <= 5 * 104
tiles[i].length == 2
1 <= li <= ri <= 109
1 <= carpetLen <= 109
The tiles are non-overlapping.
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(n)
2271. Maximum White Tiles Covered by a Carpet LeetCode Solution in C++
class Solution {
public:
int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen) {
if (ranges::any_of(tiles, [&](const auto& tile) {
return tile[1] - tile[0] + 1 >= carpetLen;
}))
return carpetLen;
int ans = 0;
vector<int> starts;
vector<int> prefix(tiles.size() + 1);
ranges::sort(tiles);
for (const vector<int>& tile : tiles)
starts.push_back(tile[0]);
for (int i = 0; i < tiles.size(); ++i) {
const int length = tiles[i][1] - tiles[i][0] + 1;
prefix[i + 1] = prefix[i] + length;
}
for (int i = 0; i < tiles.size(); ++i) {
const int s = tiles[i][0];
const int carpetEnd = s + carpetLen - 1;
const int endIndex =
ranges::upper_bound(starts, carpetEnd) - starts.begin() - 1;
const int notCover = max(0, tiles[endIndex][1] - carpetEnd);
ans = max(ans, prefix[endIndex + 1] - prefix[i] - notCover);
}
return ans;
}
};
/* code provided by PROGIEZ */
2271. Maximum White Tiles Covered by a Carpet LeetCode Solution in Java
class Solution {
public int maximumWhiteTiles(int[][] tiles, int carpetLen) {
if (Arrays.stream(tiles).anyMatch(tile -> tile[1] - tile[0] + 1 >= carpetLen))
return carpetLen;
int ans = 0;
List<Integer> starts = new ArrayList<>();
int[] prefix = new int[tiles.length + 1];
Arrays.sort(tiles, (a, b) -> Integer.compare(a[0], b[0]));
for (int[] tile : tiles)
starts.add(tile[0]);
for (int i = 0; i < tiles.length; ++i) {
final int length = tiles[i][1] - tiles[i][0] + 1;
prefix[i + 1] = prefix[i] + length;
}
for (int i = 0; i < tiles.length; ++i) {
final int s = tiles[i][0];
final int carpetEnd = s + carpetLen - 1;
final int endIndex = firstGreater(starts, carpetEnd) - 1;
final int notCover = Math.max(0, tiles[endIndex][1] - carpetEnd);
ans = Math.max(ans, prefix[endIndex + 1] - prefix[i] - notCover);
}
return ans;
}
private int firstGreater(List<Integer> A, int target) {
final int i = Collections.binarySearch(A, target + 1);
return i < 0 ? -i - 1 : i;
}
}
// code provided by PROGIEZ
2271. Maximum White Tiles Covered by a Carpet LeetCode Solution in Python
class Solution:
def maximumWhiteTiles(self, tiles: list[list[int]], carpetLen: int) -> int:
if any(tile[1] - tile[0] + 1 >= carpetLen for tile in tiles):
return carpetLen
ans = 0
prefix = [0] * (len(tiles) + 1)
tiles.sort()
starts = [tile[0] for tile in tiles]
for i, tile in enumerate(tiles):
length = tile[1] - tile[0] + 1
prefix[i + 1] = prefix[i] + length
for i, (s, _) in enumerate(tiles):
carpetEnd = s + carpetLen - 1
endIndex = bisect_right(starts, carpetEnd) - 1
notCover = max(0, tiles[endIndex][1] - carpetEnd)
ans = max(ans, prefix[endIndex + 1] - prefix[i] - notCover)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.