差分1

  1. 零数组变换 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;
    }
};
  1. 检查是否区域内所有整数都被覆盖
    题目链接: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;
    }
};
posted @ 2024-11-17 17:26  fufuaifufu  阅读(6)  评论(0编辑  收藏  举报