Skip to content

2271. Maximum White Tiles Covered by a Carpet 👍

  • Time: $O(\texttt{sort})$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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) -> 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;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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