- 零数组变换 I
题目链接:https://leetcode.cn/problems/zero-array-transformation-i/description/
题解代码:
class Solution {
public:
bool isZeroArray(vector<int>& nums, vector<vector<int>>& queries) {
int n=nums.size();
vector<int> d(n+1);
for(auto&qry:queries){
d[qry[0]]++;
d[qry[1]+1]--;
}
for(int i=0,now=0;i<n;i++){
now+=d[i];
if(now<nums[i]) return false;
}
return true;
}
};
- 检查是否区域内所有整数都被覆盖
题目链接:https://leetcode.cn/problems/check-if-all-the-integers-in-a-range-are-covered/description/
题解代码:
class Solution {
public:
bool isCovered(vector<vector<int>>& ranges, int left, int right) {
vector<int> d(52);
for(auto&range:ranges){
d[range[0]]++;
d[range[1]+1]--;
}
int cont=0;
for(int i=1;i<=50;i++){
cont+=d[i];
if(i>=left && i <= right && cont <= 0){
return false;
}
}
return true;
}
};