Advertisement

Maximum White Tiles Covered by a Carpet - LeetCode 2271 Solution

Maximum White Tiles Covered by a Carpet - Complete Solution Guide

Maximum White Tiles Covered by a Carpet is LeetCode problem 2271, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a 2D integer array tiles where tiles[i] = [l i , r i ] represents that every tile j in the range l i <= j <= r i 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 re

Detailed Explanation

The problem asks us to find the maximum number of white tiles that can be covered by a carpet of a given length. We are given a list of tile ranges, where each range [l, r] indicates that all tiles from l to r (inclusive) are white. We need to place the carpet such that it covers the maximum number of white tiles. Tiles are non-overlapping, which simplifies the problem. The carpet can be placed anywhere on the number line.

Solution Approach

The solution uses a sliding window approach after sorting the tiles based on their starting positions. For each tile ending at `rj`, we consider placing the carpet so that its right end is at `rj`. This ensures that at least one of the tile endings is fully utilized by the carpet. We then slide a window from the beginning to find all the tiles that can be covered by the carpet. While doing this, we keep track of the current coverage. When a tile is only partially covered by the carpet, we calculate that partial coverage separately.

Step-by-Step Algorithm

  1. Step 1: Sort the `tiles` array based on the starting position of each tile (i.e., the first element of each tile array).
  2. Step 2: Initialize `i` to 0 (left pointer), `current_coverage` to 0, and `max_coverage` to 0.
  3. Step 3: Iterate through the sorted `tiles` array using a right pointer `j`.
  4. Step 4: For each `j`, calculate `carpet_start = rj - carpetLen + 1`, where `rj` is the ending position of the current tile.
  5. Step 5: Update `current_coverage` by adding the length of the current tile (`rj - lj + 1`).
  6. Step 6: Move the left pointer `i` forward until `tiles[i][0] >= carpet_start`. While doing this, subtract the length of the tiles that are no longer fully covered (`ri - li + 1`) from `current_coverage`.
  7. Step 7: Calculate `fully_covered_len` as the current `current_coverage` which represents coverage from `tiles[i]` to `tiles[j]`.
  8. Step 8: Calculate `partial_coverage`. Check if `i > 0`. If so, determine how much of the tile at `tiles[i-1]` is covered by the carpet that starts at `carpet_start`, but ends at the current `rj`. Add that to the `partial_coverage`.
  9. Step 9: Update `max_coverage = max(max_coverage, fully_covered_len + partial_coverage)`. This stores the maximum coverage we have seen so far.
  10. Step 10: After iterating through all tiles, return `max_coverage`.

Key Insights

  • Insight 1: Sorting the tiles allows us to efficiently use a sliding window approach.
  • Insight 2: The optimal placement of the carpet ends on the right edge of one of the tiles. This significantly reduces the search space.
  • Insight 3: We need to handle partially covered tiles, meaning the carpet might not fully cover every tile within the sliding window.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Binary Search, Greedy, Sliding Window, Sorting, Prefix Sum.

Companies

Asked at: LTI.