Skip to content

Commit 7110683

Browse files
committed
+ problem 2271
1 parent 88c28b6 commit 7110683

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 2271. Maximum White Tiles Covered by a Carpet
2+
You are given a 2D integer array `tiles` where <code>tiles[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> represents that every tile `j` in the range <code>l<sub>i</sub> <= j <= r<sub>i</sub></code> is colored white.
3+
4+
You are also given an integer `carpetLen`, the length of a single carpet that can be placed **anywhere**.
5+
6+
Return *the **maximum** number of white tiles that can be covered by the carpet*.
7+
8+
#### Example 1:
9+
![](https://assets.leetcode.com/uploads/2022/03/25/example1drawio3.png)
10+
<pre>
11+
<strong>Input:</strong> tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
12+
<strong>Output:</strong> 9
13+
<strong>Explanation:</strong> Place the carpet starting on tile 10.
14+
It covers 9 white tiles, so we return 9.
15+
Note that there may be other places where the carpet covers 9 white tiles.
16+
It can be shown that the carpet cannot cover more than 9 white tiles.
17+
</pre>
18+
19+
#### Example 2:
20+
![](https://assets.leetcode.com/uploads/2022/03/24/example2drawio.png)
21+
<pre>
22+
<strong>Input:</strong> tiles = [[10,11],[1,1]], carpetLen = 2
23+
<strong>Output:</strong> 2
24+
<strong>Explanation:</strong> Place the carpet starting on tile 10.
25+
It covers 2 white tiles, so we return 2.
26+
</pre>
27+
28+
#### Constraints:
29+
* <code>1 <= tiles.length <= 5 * 10<sup>4</sup></code>
30+
* `tiles[i].length == 2`
31+
* <code>1 <= l<sub>i</sub> <= r<sub>i</sub> <= 10<sup>9</sup></code>
32+
* <code>1 <= carpetLen <= 10<sup>9</sup></code>
33+
* The `tiles` are **non-overlapping**.
34+
35+
## Solutions (Rust)
36+
37+
### 1. Solution
38+
```Rust
39+
impl Solution {
40+
pub fn maximum_white_tiles(tiles: Vec<Vec<i32>>, carpet_len: i32) -> i32 {
41+
let mut tiles = tiles;
42+
let mut prefix_sum = vec![0; tiles.len() + 1];
43+
let mut ret = 0;
44+
45+
tiles.sort_unstable();
46+
47+
for i in 1..prefix_sum.len() {
48+
prefix_sum[i] = prefix_sum[i - 1] + tiles[i - 1][1] - tiles[i - 1][0] + 1;
49+
}
50+
51+
for i in 0..tiles.len() {
52+
let j = tiles
53+
.binary_search(&vec![tiles[i][0] + carpet_len, i32::MAX])
54+
.unwrap_err();
55+
if tiles[j - 1][1] < tiles[i][0] + carpet_len {
56+
ret = ret.max(prefix_sum[j] - prefix_sum[i]);
57+
} else {
58+
ret = ret.max(
59+
prefix_sum[j] - prefix_sum[i] - tiles[j - 1][1] - tiles[i][0] - carpet_len,
60+
);
61+
}
62+
let j = tiles
63+
.binary_search(&vec![tiles[i][1] - carpet_len, i32::MAX])
64+
.unwrap_err();
65+
if j == 0 || tiles[j - 1][1] <= tiles[i][1] - carpet_len {
66+
ret = ret.max(prefix_sum[i + 1] - prefix_sum[j]);
67+
} else {
68+
ret = ret.max(
69+
prefix_sum[i + 1] - prefix_sum[j] + tiles[j - 1][1] - tiles[i][1] + carpet_len,
70+
);
71+
}
72+
}
73+
74+
ret
75+
}
76+
}
77+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 2271. 毯子覆盖的最多白色砖块数
2+
给你一个二维整数数组 `tiles` ,其中 <code>tiles[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> ,表示所有在 <code>l<sub>i</sub> <= j <= r<sub>i</sub></code> 之间的每个瓷砖位置 `j` 都被涂成了白色。
3+
4+
同时给你一个整数 `carpetLen` ,表示可以放在 **任何位置** 的一块毯子的长度。
5+
6+
请你返回使用这块毯子,**最多** 可以盖住多少块瓷砖。
7+
8+
#### 示例 1:
9+
![](https://assets.leetcode.com/uploads/2022/03/25/example1drawio3.png)
10+
<pre>
11+
<strong>输入:</strong> tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
12+
<strong>输出:</strong> 9
13+
<strong>解释:</strong> 将毯子从瓷砖 10 开始放置。
14+
总共覆盖 9 块瓷砖,所以返回 9 。
15+
注意可能有其他方案也可以覆盖 9 块瓷砖。
16+
可以看出,瓷砖无法覆盖超过 9 块瓷砖。
17+
</pre>
18+
19+
#### 示例 2:
20+
![](https://assets.leetcode.com/uploads/2022/03/24/example2drawio.png)
21+
<pre>
22+
<strong>输入:</strong> tiles = [[10,11],[1,1]], carpetLen = 2
23+
<strong>输出:</strong> 2
24+
<strong>解释:</strong> 将毯子从瓷砖 10 开始放置。
25+
总共覆盖 2 块瓷砖,所以我们返回 2 。
26+
</pre>
27+
28+
#### 提示:
29+
* <code>1 <= tiles.length <= 5 * 10<sup>4</sup></code>
30+
* `tiles[i].length == 2`
31+
* <code>1 <= l<sub>i</sub> <= r<sub>i</sub> <= 10<sup>9</sup></code>
32+
* <code>1 <= carpetLen <= 10<sup>9</sup></code>
33+
* `tiles` 互相 **不会重叠**
34+
35+
## 题解 (Rust)
36+
37+
### 1. 题解
38+
```Rust
39+
impl Solution {
40+
pub fn maximum_white_tiles(tiles: Vec<Vec<i32>>, carpet_len: i32) -> i32 {
41+
let mut tiles = tiles;
42+
let mut prefix_sum = vec![0; tiles.len() + 1];
43+
let mut ret = 0;
44+
45+
tiles.sort_unstable();
46+
47+
for i in 1..prefix_sum.len() {
48+
prefix_sum[i] = prefix_sum[i - 1] + tiles[i - 1][1] - tiles[i - 1][0] + 1;
49+
}
50+
51+
for i in 0..tiles.len() {
52+
let j = tiles
53+
.binary_search(&vec![tiles[i][0] + carpet_len, i32::MAX])
54+
.unwrap_err();
55+
if tiles[j - 1][1] < tiles[i][0] + carpet_len {
56+
ret = ret.max(prefix_sum[j] - prefix_sum[i]);
57+
} else {
58+
ret = ret.max(
59+
prefix_sum[j] - prefix_sum[i] - tiles[j - 1][1] - tiles[i][0] - carpet_len,
60+
);
61+
}
62+
let j = tiles
63+
.binary_search(&vec![tiles[i][1] - carpet_len, i32::MAX])
64+
.unwrap_err();
65+
if j == 0 || tiles[j - 1][1] <= tiles[i][1] - carpet_len {
66+
ret = ret.max(prefix_sum[i + 1] - prefix_sum[j]);
67+
} else {
68+
ret = ret.max(
69+
prefix_sum[i + 1] - prefix_sum[j] + tiles[j - 1][1] - tiles[i][1] + carpet_len,
70+
);
71+
}
72+
}
73+
74+
ret
75+
}
76+
}
77+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
impl Solution {
2+
pub fn maximum_white_tiles(tiles: Vec<Vec<i32>>, carpet_len: i32) -> i32 {
3+
let mut tiles = tiles;
4+
let mut prefix_sum = vec![0; tiles.len() + 1];
5+
let mut ret = 0;
6+
7+
tiles.sort_unstable();
8+
9+
for i in 1..prefix_sum.len() {
10+
prefix_sum[i] = prefix_sum[i - 1] + tiles[i - 1][1] - tiles[i - 1][0] + 1;
11+
}
12+
13+
for i in 0..tiles.len() {
14+
let j = tiles
15+
.binary_search(&vec![tiles[i][0] + carpet_len, i32::MAX])
16+
.unwrap_err();
17+
if tiles[j - 1][1] < tiles[i][0] + carpet_len {
18+
ret = ret.max(prefix_sum[j] - prefix_sum[i]);
19+
} else {
20+
ret = ret.max(
21+
prefix_sum[j] - prefix_sum[i] - tiles[j - 1][1] - tiles[i][0] - carpet_len,
22+
);
23+
}
24+
let j = tiles
25+
.binary_search(&vec![tiles[i][1] - carpet_len, i32::MAX])
26+
.unwrap_err();
27+
if j == 0 || tiles[j - 1][1] <= tiles[i][1] - carpet_len {
28+
ret = ret.max(prefix_sum[i + 1] - prefix_sum[j]);
29+
} else {
30+
ret = ret.max(
31+
prefix_sum[i + 1] - prefix_sum[j] + tiles[j - 1][1] - tiles[i][1] + carpet_len,
32+
);
33+
}
34+
}
35+
36+
ret
37+
}
38+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,7 @@
12041204
[2266][2266l]|[Count Number of Texts][2266] |![py]
12051205
[2269][2269l]|[Find the K-Beauty of a Number][2269] |![py]
12061206
[2270][2270l]|[Number of Ways to Split Array][2270] |![rs]
1207+
[2271][2271l]|[Maximum White Tiles Covered by a Carpet][2271] |![rs]
12071208
[2273][2273l]|[Find Resultant Array After Removing Anagrams][2273] |![py]
12081209
[2274][2274l]|[Maximum Consecutive Floors Without Special Floors][2274] |![rs]
12091210
[2275][2275l]|[Largest Combination With Bitwise AND Greater Than Zero][2275] |![rs]
@@ -2555,6 +2556,7 @@
25552556
[2266]:Problemset/2266-Count%20Number%20of%20Texts/README.md#2266-count-number-of-texts
25562557
[2269]:Problemset/2269-Find%20the%20K-Beauty%20of%20a%20Number/README.md#2269-find-the-k-beauty-of-a-number
25572558
[2270]:Problemset/2270-Number%20of%20Ways%20to%20Split%20Array/README.md#2270-number-of-ways-to-split-array
2559+
[2271]:Problemset/2271-Maximum%20White%20Tiles%20Covered%20by%20a%20Carpet/README.md#2271-maximum-white-tiles-covered-by-a-carpet
25582560
[2273]:Problemset/2273-Find%20Resultant%20Array%20After%20Removing%20Anagrams/README.md#2273-find-resultant-array-after-removing-anagrams
25592561
[2274]:Problemset/2274-Maximum%20Consecutive%20Floors%20Without%20Special%20Floors/README.md#2274-maximum-consecutive-floors-without-special-floors
25602562
[2275]:Problemset/2275-Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/README.md#2275-largest-combination-with-bitwise-and-greater-than-zero
@@ -3909,6 +3911,7 @@
39093911
[2266l]:https://leetcode.com/problems/count-number-of-texts/
39103912
[2269l]:https://leetcode.com/problems/find-the-k-beauty-of-a-number/
39113913
[2270l]:https://leetcode.com/problems/number-of-ways-to-split-array/
3914+
[2271l]:https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/
39123915
[2273l]:https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/
39133916
[2274l]:https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/
39143917
[2275l]:https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,7 @@
12041204
[2266][2266l]|[统计打字方案数][2266] |![py]
12051205
[2269][2269l]|[找到一个数字的 K 美丽值][2269] |![py]
12061206
[2270][2270l]|[分割数组的方案数][2270] |![rs]
1207+
[2271][2271l]|[毯子覆盖的最多白色砖块数][2271] |![rs]
12071208
[2273][2273l]|[移除字母异位词后的结果数组][2273] |![py]
12081209
[2274][2274l]|[不含特殊楼层的最大连续楼层数][2274] |![rs]
12091210
[2275][2275l]|[按位与结果大于零的最长组合][2275] |![rs]
@@ -2555,6 +2556,7 @@
25552556
[2266]:Problemset/2266-Count%20Number%20of%20Texts/README_CN.md#2266-统计打字方案数
25562557
[2269]:Problemset/2269-Find%20the%20K-Beauty%20of%20a%20Number/README_CN.md#2269-找到一个数字的-k-美丽值
25572558
[2270]:Problemset/2270-Number%20of%20Ways%20to%20Split%20Array/README_CN.md#2270-分割数组的方案数
2559+
[2271]:Problemset/2271-Maximum%20White%20Tiles%20Covered%20by%20a%20Carpet/README_CN.md#2271-毯子覆盖的最多白色砖块数
25582560
[2273]:Problemset/2273-Find%20Resultant%20Array%20After%20Removing%20Anagrams/README_CN.md#2273-移除字母异位词后的结果数组
25592561
[2274]:Problemset/2274-Maximum%20Consecutive%20Floors%20Without%20Special%20Floors/README_CN.md#2274-不含特殊楼层的最大连续楼层数
25602562
[2275]:Problemset/2275-Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/README_CN.md#2275-按位与结果大于零的最长组合
@@ -3909,6 +3911,7 @@
39093911
[2266l]:https://leetcode.cn/problems/count-number-of-texts/
39103912
[2269l]:https://leetcode.cn/problems/find-the-k-beauty-of-a-number/
39113913
[2270l]:https://leetcode.cn/problems/number-of-ways-to-split-array/
3914+
[2271l]:https://leetcode.cn/problems/maximum-white-tiles-covered-by-a-carpet/
39123915
[2273l]:https://leetcode.cn/problems/find-resultant-array-after-removing-anagrams/
39133916
[2274l]:https://leetcode.cn/problems/maximum-consecutive-floors-without-special-floors/
39143917
[2275l]:https://leetcode.cn/problems/largest-combination-with-bitwise-and-greater-than-zero/

0 commit comments

Comments
 (0)