Cover the Most White Integer Tiles with One Carpet

Quick Overview

Place one fixed-length carpet over nonoverlapping white-tile intervals and return the maximum number of covered integer positions.

Cover the Most White Integer Tiles with One Carpet

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Each inclusive interval in `tiles` colors its integer positions white, and the intervals do not overlap. Place one carpet covering carpetLen consecutive integer positions anywhere. Return the greatest number of white tiles it can cover; only the count is required. ```cpp class Solution { public: int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen); }; ``` ### Examples **Example 1:** ```text Input: tiles = [[2,4],[8,10]], carpetLen = 4 Output: 3 Explanation: Either three-position white interval can be fully covered. A length-four carpet cannot span the gap and cover four white positions. ``` **Example 2:** ```text Input: tiles = [[4,8]], carpetLen = 3 Output: 3 Explanation: Any three consecutive positions within the five-position interval can be covered. ``` ### Constraints - `1 <= tiles.length <= 50000`. - Every interval has two integers with `1 <= left <= right <= 10^9`. - `1 <= carpetLen <= 10^9`. - Intervals are non-overlapping. ### Clarifying Questions - Are interval endpoints included? Yes; `[l, r]` contains `r - l + 1` tiles. - Can the carpet cross gaps? Yes, but nonwhite positions contribute nothing. - Must the carpet stay inside one white interval? No; place it anywhere. - Is a placement required in the result? No; return only the greatest covered count. ```hint Check the boundary A carpet of length `k` starting at `x` ends at `x + k - 1`, not `x + k`. ```

Overview: Place one fixed-length carpet over nonoverlapping white-tile intervals and return the maximum number of covered integer positions.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each inclusive interval in tiles colors its integer positions white, and the intervals do not overlap. Place one carpet covering carpetLen consecutive integer positions anywhere. Return the greatest number of white tiles it can cover; only the count is required.

class Solution {
public:
    int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen);
};

Examples

Example 1:

Input: tiles = [[2,4],[8,10]], carpetLen = 4
Output: 3
Explanation: Either three-position white interval can be fully covered. A length-four carpet cannot span the gap and cover four white positions.

Example 2:

Input: tiles = [[4,8]], carpetLen = 3
Output: 3
Explanation: Any three consecutive positions within the five-position interval can be covered.

Constraints

  • 1 <= tiles.length <= 50000 .
  • Every interval has two integers with 1 <= left <= right <= 10^9 .
  • 1 <= carpetLen <= 10^9 .
  • Intervals are non-overlapping.

Clarifying Questions Guidance

  • Are interval endpoints included? Yes; [l, r] contains r - l + 1 tiles.
  • Can the carpet cross gaps? Yes, but nonwhite positions contribute nothing.
  • Must the carpet stay inside one white interval? No; place it anywhere.
  • Is a placement required in the result? No; return only the greatest covered count.