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 * 104tiles[i].length == 21 <= li <= ri <= 1091 <= carpetLen <= 109tiles are non-overlapping.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this problem is all about trying every single possible placement of the carpet. We will check the number of white tiles covered for each of these placements. Then, we simply pick the carpet placement that covers the most white tiles.
Here's how the algorithm would work step-by-step:
def maximum_white_tiles_covered_brute_force(tiles, carpet_length):
maximum_covered_tiles = 0
number_of_tiles = len(tiles)
# Iterate through all possible starting positions of the carpet.
for carpet_start_index in range(number_of_tiles):
carpet_start_position = tiles[carpet_start_index][0]
# Consider all possible start positions between tiles.
while carpet_start_position <= tiles[-1][1]:
covered_tiles = 0
# Iterate through all tiles and check how many are covered.
for tile_start, tile_end in tiles:
# Calculate the overlap between the carpet and the current tile.
overlap_start = max(carpet_start_position, tile_start)
overlap_end = min(carpet_start_position + carpet_length, tile_end)
# If there is a positive overlap, add it to the count.
overlap = max(0, overlap_end - overlap_start)
covered_tiles += overlap
# Update the maximum number of covered tiles.
maximum_covered_tiles = max(maximum_covered_tiles, covered_tiles)
# Move carpet one position to the right
carpet_start_position += 1
return maximum_covered_tilesThe core idea is to efficiently determine the maximum tiles covered by the carpet by sliding it across the tiles. Instead of exhaustively checking every possible placement, we leverage a sliding window technique combined with precomputed sums to find the optimal position.
Here's how the algorithm would work step-by-step:
def maximum_white_tiles(tiles, carpet_length):
tiles.sort()
tile_lengths = [tile[1] - tile[0] + 1 for tile in tiles]
max_covered = 0
window_start = 0
current_covered = 0
for window_end in range(len(tiles)):
current_covered += tile_lengths[window_end]
# This loop maintains the sliding window within carpet_length.
while tiles[window_end][1] - tiles[window_start][0] + 1 > carpet_length:
current_covered -= tile_lengths[window_start]
window_start += 1
# Need to recalc the covered tiles within the carpet length
covered_by_carpet = current_covered
left_over_space = carpet_length - (tiles[window_end][1] - tiles[window_start][0] + 1)
if left_over_space < 0:
covered_by_carpet = current_covered + left_over_space
#Keep track of maximum tiles covered
max_covered = max(max_covered, covered_by_carpet)
return max_covered| Case | How to Handle |
|---|---|
| Empty tiles array | Return 0 immediately as there are no tiles to cover. |
| Carpet length is 0 | Return 0 immediately as no tiles can be covered with a carpet of length 0. |
| Carpet length is larger than the range of all tiles | Return the sum of the lengths of all tiles, since the carpet can cover everything. |
| Tiles array has only one tile | Return the length of the single tile, or the carpet length if it's smaller. |
| Tiles are very sparse (large gaps between them) | Sliding window approach will handle large gaps correctly, potentially skipping many iterations. |
| Tiles are clustered together (minimal gaps) | Sliding window will still work efficiently as the covered tiles will increase mostly linearly with the carpet length. |
| Large tile start and end values leading to integer overflow in calculations | Use long data type to store tile start and end values, and intermediate calculations, preventing overflow issues. |
| Tiles are not sorted by starting position | Sort the tiles array based on their starting positions before applying the sliding window algorithm. |