返回介绍

solution / 2200-2299 / 2271.Maximum White Tiles Covered by a Carpet / README_EN

发布于 2024-06-17 01:03:08 字数 4982 浏览 0 评论 0 收藏 0

2271. Maximum White Tiles Covered by a Carpet

中文文档

Description

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 * 104
  • tiles[i].length == 2
  • 1 <= li <= ri <= 109
  • 1 <= carpetLen <= 109
  • The tiles are non-overlapping.

Solutions

Solution 1

class Solution:
  def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int:
    tiles.sort()
    n = len(tiles)
    s = ans = j = 0
    for i, (li, ri) in enumerate(tiles):
      while j < n and tiles[j][1] - li + 1 <= carpetLen:
        s += tiles[j][1] - tiles[j][0] + 1
        j += 1
      if j < n and li + carpetLen > tiles[j][0]:
        ans = max(ans, s + li + carpetLen - tiles[j][0])
      else:
        ans = max(ans, s)
      s -= ri - li + 1
    return ans
class Solution {
  public int maximumWhiteTiles(int[][] tiles, int carpetLen) {
    Arrays.sort(tiles, (a, b) -> a[0] - b[0]);
    int n = tiles.length;
    int s = 0, ans = 0;
    for (int i = 0, j = 0; i < n; ++i) {
      while (j < n && tiles[j][1] - tiles[i][0] + 1 <= carpetLen) {
        s += tiles[j][1] - tiles[j][0] + 1;
        ++j;
      }
      if (j < n && tiles[i][0] + carpetLen > tiles[j][0]) {
        ans = Math.max(ans, s + tiles[i][0] + carpetLen - tiles[j][0]);
      } else {
        ans = Math.max(ans, s);
      }
      s -= (tiles[i][1] - tiles[i][0] + 1);
    }
    return ans;
  }
}
class Solution {
public:
  int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen) {
    sort(tiles.begin(), tiles.end());
    int s = 0, ans = 0, n = tiles.size();
    for (int i = 0, j = 0; i < n; ++i) {
      while (j < n && tiles[j][1] - tiles[i][0] + 1 <= carpetLen) {
        s += tiles[j][1] - tiles[j][0] + 1;
        ++j;
      }
      if (j < n && tiles[i][0] + carpetLen > tiles[j][0]) {
        ans = max(ans, s + tiles[i][0] + carpetLen - tiles[j][0]);
      } else {
        ans = max(ans, s);
      }
      s -= (tiles[i][1] - tiles[i][0] + 1);
    }
    return ans;
  }
};
func maximumWhiteTiles(tiles [][]int, carpetLen int) int {
  sort.Slice(tiles, func(i, j int) bool { return tiles[i][0] < tiles[j][0] })
  n := len(tiles)
  s, ans := 0, 0
  for i, j := 0, 0; i < n; i++ {
    for j < n && tiles[j][1]-tiles[i][0]+1 <= carpetLen {
      s += tiles[j][1] - tiles[j][0] + 1
      j++
    }
    if j < n && tiles[i][0]+carpetLen > tiles[j][0] {
      ans = max(ans, s+tiles[i][0]+carpetLen-tiles[j][0])
    } else {
      ans = max(ans, s)
    }
    s -= (tiles[i][1] - tiles[i][0] + 1)
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文