LeetCode 2271: Maximum White Tiles Covered by a Carpet

Problem Description

Explanation:

To solve this problem, we can iterate over the given tiles and calculate the coverage for each possible starting position of the carpet. For each starting position, we calculate the number of white tiles covered by the carpet. We keep track of the maximum number of white tiles covered and return that as the result.

  1. Sort the tiles based on their starting positions.
  2. Iterate over the sorted tiles:
    • For each tile, calculate the coverage of the carpet starting at this tile.
    • Update the maximum coverage if the current coverage is greater.
  3. Return the maximum coverage.

Time Complexity: O(n log n) where n is the number of tiles. Sorting the tiles takes O(n log n) time complexity.

Space Complexity: O(1) as we are using constant extra space.

:

Solutions

class Solution {
    public int maxWhiteTilesCovered(int[][] tiles, int carpetLen) {
        Arrays.sort(tiles, (a, b) -> a[0] - b[0]);
        int maxCovered = 0;
        for (int[] tile : tiles) {
            int start = tile[0];
            int end = tile[1];
            int coverage = Math.min(end - start + 1, carpetLen);
            maxCovered = Math.max(maxCovered, coverage);
        }
        return maxCovered;
    }
}

Loading editor...