当前位置:网站首页>2271. 毯子覆盖的最多白色砖块数 ●●
2271. 毯子覆盖的最多白色砖块数 ●●
2022-07-25 13:52:00 【keep_fan】
2271. 毯子覆盖的最多白色砖块数 ●●
描述
给你一个二维整数数组 tiles ,其中 t i l e s [ i ] = [ l i , r i ] tiles[i] = [l_i, r_i] tiles[i]=[li,ri] ,表示所有在 l i < = j < = r i l_i <= j <= r_i li<=j<=ri 之间的每个瓷砖位置 j 都被涂成了白色。
同时给你一个整数 carpetLen ,表示可以放在 任何位置 的一块毯子。
请你返回使用这块毯子,最多 可以盖住多少块瓷砖。
示例
输入:tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
输出:9
解释:将毯子从瓷砖 10 开始放置。
总共覆盖 9 块瓷砖,所以返回 9 。
注意可能有其他方案也可以覆盖 9 块瓷砖。
可以看出,瓷砖无法覆盖超过 9 块瓷砖。
题解
1. 双指针 + 滑动窗口
核心思路:滑动窗口,右指针不断扩大,直到不能继续后移(该段不是完全被覆盖的情况),收缩左指针,让右指针再次尝试扩大。
毯子覆盖情况:
- 完全覆盖当前 right 段,则记录长度,右指针右移,计算更多覆盖的长度;
- 覆盖部分 right 段,记录长度,毯子(左指针)右移;
- 完全不覆盖当前 right 段,毯子(左指针)右移。
- 时间复杂度: O ( n log n ) O(n\log n) O(nlogn),其中 n 为 tiles 的长度。对 tiles 数组排序的时间复杂度为 O ( n log n ) O(n\log n) O(nlogn),双指针维护最多可覆盖数量的时间复杂度为 O(n)。
- 空间复杂度: O ( log n ) O(\log n) O(logn),即为排序的栈空间开销。
class Solution {
public:
int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen) {
sort(tiles.begin(), tiles.end(), [](vector<int>& a, vector<int>& b){
return a[0] < b[0]; // 开头升序排序
});
int n = tiles.size();
int left = 0, right = 0, len = 0, ans = 0;
while(left <= right && right < n){
int rightMax = tiles[left][0] + carpetLen - 1;
if(tiles[right][1] <= rightMax){
// 整段覆盖
len += tiles[right][1] - tiles[right][0] + 1;
ans = max(ans, len);
++right; // 该段全部覆盖的情况下,才继续移动右指针,否则移动左指针
}else{
// 非整段覆盖:该段部分覆盖 / 该段完全不覆盖
if(tiles[right][0] <= rightMax){
// 部分覆盖
ans = max(ans, len + rightMax - tiles[right][0] + 1); // 因为接下来要移动左指针,因此直接比较当前部分覆盖情况下的最长长度
if(ans >= carpetLen) return carpetLen;
}
// 移动毯子起始位置到下一区间开头
len -= tiles[left][1] - tiles[left][0] + 1;
++left;
}
}
return ans;
}
};
边栏推荐
- 2022年下半年软考信息安全工程师如何备考?
- Concurrent tool set for concurrent programming
- sieve of eratosthenes
- How to refactor embedded code?
- stable_ Baselines quick start
- Brush questions - Luogu -p1161 turn on the light
- [force buckle] 645. Wrong set
- Redux usage and analysis
- Tm1638 LED digital display module Arduino drive code
- Working mode and sleep mode of nlm5 series wireless vibrating wire sensor acquisition instrument
猜你喜欢

What is your revenue rank among global developers in 2022?

Brush questions - Luogu -p1059 clear random number

adb通过Wi-Fi连接小米手机

Working principle of Lora to 4G and gateway repeater

Install oh my Zsh

Brush questions - Luogu -p1151 sub number integer

Brush questions - Luogu -p1161 turn on the light

Applet H5 get mobile number scheme

在线问题反馈模块实战(十三):实现多参数分页查询列表

What are the ranking strategies for mobile websites, independent apps and websites?
随机推荐
Hcip day 8 notes
[force deduction] 1030. Arrange matrix cells in distance order
Azure Devops (XIV) use azure's private nuget warehouse
Redux usage and analysis
Applet sharing function
2022年下半年软考信息安全工程师如何备考?
Dr. Berkeley's "machine learning engineering" big truth; AI vice president '2022 ml job market' analysis; Large list of semiconductor start-ups; Large scale video face attribute data set; Cutting edge
Uncaught SyntaxError: Octal literals are not allowed in strict mode.
Congestion control of TCP
Tm1637 four digit LED display module Arduino driver with second dot
刷题-洛谷-P1161 开灯
hcip第八天实验
在线问题反馈模块实战(十三):实现多参数分页查询列表
Brush questions - Luogu -p1151 sub number integer
pytest.mark.parametrize及mock使用
命名空间与库
MySQL 01: Source command
刷题-洛谷-P1046 陶陶摘苹果
ES6 array de duplication new set()
Applet enterprise red envelope function
