首页 > 其他分享 >花期内花的数目

花期内花的数目

时间:2023-09-28 23:44:45浏览次数:32  
标签:begin flower int 内花 花期 persons vector flowers 数目

给你一个下标从 0 开始的二维整数数组 flowers
其中 flowers[i] = [starti, endi] 表示第 i 朵花的花期从 starti 到 endi
同时给你一个下标从 0 开始大小为 n 的整数数组 people ,people[i] 是第 i 个人来看花的时间
请你返回一个大小为 n 的整数数组 answer ,其中 answer[i]是第 i 个人到达时在花期内花的数目

1. 合并排序 + 离线查询

插入点和查询点合并

class Solution {
public:
    vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& people) {
        //离线查询,分别标识天数,开花、人、死掉,以及人对应的位置结果标识
        vector<vector<int>> search;
        int curflower = 0;//记录当前花的数目
        for(auto &flower:flowers){
            search.push_back(vector<int>{flower[0],1,-1});
            search.push_back(vector<int>{flower[1],3,-1});
        }
        for(int i=0;i<people.size();i++)
            search.push_back(vector<int>{people[i],2,i});
        sort(search.begin(),search.end(),[&](const vector<int>&a,const vector<int>&b ){
            if(a[0]==b[0])  return a[1]<b[1];
            return a[0]<b[0];
        });
        vector<int> res(people.size());
        for(auto &s:search){
            if(s[1]==1) curflower++;
            if(s[1]==2) res[s[2]] = curflower;
            if(s[1]==3) curflower--;
        }
        return res;
    }
};

2. 不合并排序 + 离线查询

插入点和查询点不合并

class Solution {
public:
    vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& people) {
        //离线查询,分别标识天数,开花、人、死掉,以及人对应的位置结果标识
        vector<vector<int>> search;
        int curflower = 0;//记录当前花的数目
        for(auto &flower:flowers){
            search.push_back(vector<int>{flower[0],1});
            search.push_back(vector<int>{flower[1]+1,-1});
        }
        sort(search.begin(),search.end());
        vector<int> index(people.size());
        iota(index.begin(),index.end(),0);
        sort(index.begin(),index.end(),[&](int a,int b){
            return people[a]<people[b];
        });
        int sidx = 0;
        vector<int> res(people.size());
        for(auto idx:index){
            while(sidx<search.size()&&search[sidx][0]<=people[idx])
                curflower+=search[sidx++][1];
            res[idx] = curflower;
        }
        return res;
    }
};

3. 差分 + 离线查询

本质上是利用红黑树的有序,进行查询

class Solution {
public:
    vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& persons) {
        map<int, int> cnt;
        for (auto &flower : flowers) {
            cnt[flower[0]]++;
            cnt[flower[1] + 1]--;
        }
        int n = persons.size();
        vector<int> indices(n);
        iota(indices.begin(), indices.end(), 0);
        sort(indices.begin(), indices.end(), [&](int a, int b) {
            return persons[a] < persons[b];
        });

        vector<int> ans(n);
        int curr = 0;
        auto it = cnt.begin();
        for (int x : indices) {
            while (it != cnt.end() && it->first <= persons[x]) {
                curr += it->second;
                it++;
            }
            ans[x] = curr;
        }
        return ans;
    }
};

4. 排序 + 二分

直接二分求开花和落花的个数

class Solution {
public:
    vector<int> fullBloomFlowers(vector<vector<int>> &flowers, vector<int> &persons) {
        int n = flowers.size();
        vector<int> starts(n), ends(n);
        for (int i = 0; i < n; ++i) {
            starts[i] = flowers[i][0];
            ends[i] = flowers[i][1];
        }
        sort(starts.begin(), starts.end());
        sort(ends.begin(), ends.end());
        int m = persons.size();
        vector<int> ans(m);
        for (int i = 0; i < m; ++i) {
            int x = upper_bound(starts.begin(), starts.end(), persons[i]) - starts.begin();
            int y = lower_bound(ends.begin(), ends.end(), persons[i]) - ends.begin();
            ans[i] = x - y;
        }
        return ans;
    }
};

5. 离散化 + 树状数组

class Solution {
public:
    vector<int> fullBloomFlowers(vector<vector<int>> &flowers, vector<int> &persons) {
        map<int,int> m;
        //进行离散化
        for(auto &flower:flowers){
            m[flower[0]]++;
            m[flower[1]+1]++;
        }
        for(auto person:persons)
            m[person]++;
        int idx = 1;
        for(auto &[key,val]:m)
            val = idx++;
        //初始化树
        n = m.size();
        tree.resize(n+1,0);
        //对插入点进行插入更新
        
       for(auto &flower:flowers){
            update(m[flower[0]],1);
            update(m[flower[1]+1],-1);
        }
        //进行查询
        for(auto &person:persons)
            person = getsum(m[person]);
        return persons;
    }

    vector<int> tree;
    int n;
    int lowbit(int x){//求二进制化最后一位的值
        return x&(-x);
    }
    void update(int i,int k){ //在i位置加上k,O(logn)复杂度单点修改
        while(i<=n){//更新子树上所有值
            tree[i]+=k;
            i+=lowbit(i);//移动到父亲节点
        }
    }

    long long getsum(int i){  //求数组前i项的和
        long long res=0;
        while(i>0){//O(logn)求前缀和
            res+=tree[i];
            i-=lowbit(i);//移动到前一棵子树(子区间)
        }
        return res;
    }
};


标签:begin,flower,int,内花,花期,persons,vector,flowers,数目
From: https://www.cnblogs.com/929code/p/17736666.html

相关文章

  • [LeetCode] 2251. 花期内花的数目 - 二分查找/有序数组
    Problem:2251.花期内花的数目思路看题目应该是一道比较经典的差分,本来准备拿差分数组做的,后来搂了一眼题解,发现用二分的方法更简单解题方法此题有一种很简便的方法,第i个人到达时间为people[i],所以我们不难找到在这个时间之前花期已经开始的花的数量,即v1=start<=people[i]......
  • LC2251 花期内花的数目
    方法一:差分因为是先修改后查询,很容易想到差分,但因为数据值域\([-10^9,10^9]\)过大,所以不能使用差分数组,而应用map进行存储,如代码所示:map<int,int>diff;//正常进行差分操作for(auto&f:flowers){diff[f[0]]++;diff[f[1]+1]--;}//dosomethingautoit......
  • 力扣-2798-满足目标工作时长的员工数目
    公司里共有n名员工,按从0到n-1编号。每个员工i已经在公司工作了hours[i]小时。公司要求每位员工工作至少target小时。给你一个下标从0开始、长度为n的非负整数数组hours和一个非负整数target。请你用整数表示并返回工作至少target小时的员工数。 示......
  • linux 中批量输出指定目录的磁盘占用和文件数目
     001、磁盘占用(base)[root@pc1test1]#lstest1test2test3(base)[root@pc1test1]#find$PWD-typed##查出所有目录/home/test1/home/test1/test1/home/test1/test1/test/home/test1/test2/home/test1/test3(base)[root@pc1test1]#find$PWD......
  • [LeetCode] 1353. Maximum Number of Events That Can Be Attended 最多可以参加的会
    Youaregivenanarrayof events where events[i]=[startDayi,endDayi].Everyevent i startsat startDayi andendsat endDayi.Youcanattendanevent i atanyday d where startTimei<=d<=endTimei.Youcanonlyattendoneeventatanytime ......
  • 【Leetcode刷题记录】1、统计参与通信的服务器;2、统计二叉树中好节点的数目;3、从两个
    1、统计参与通信的服务器题目:这里有一幅服务器分布图,服务器的位置标识在 m*n 的整数矩阵网格 grid 中,1表示单元格上有服务器,0表示没有。如果两台服务器位于同一行或者同一列,我们就认为它们之间可以进行通信。请你统计并返回能够与至少一台其他服务器进行通信的服务器的......
  • 2845. 统计趣味子数组的数目-361
    2845.统计趣味子数组的数目给你一个下标从0开始的整数数组nums,以及整数modulo和整数k。请你找出并统计数组中趣味子数组的数目。如果子数组nums[l..r]满足下述条件,则称其为趣味子数组:在范围[l,r]内,设cnt为满足nums[i]%modulo==k的索引i的数量。......
  • 统计一个字符串的 k 子序列美丽值最大的数目
    k子序列指的是s的一个长度为k的子序列,且所有字符都是唯一的,也就是说每个字符在子序列里只出现过一次。定义f(c)为字符c在s中出现的次数。k子序列的美丽值定义为这个子序列中每一个字符c的f(c)之和1.贪心+组合枚举贪心选美丽值最大的字符,对于最后美丽值相......
  • python中计算点突变的数目
     001、直接比较计算[root@PC1test01]#lsa.fab.fatest.py[root@PC1test01]#cata.fa##测试dna序列GAGCCTACTAACGGGAT[root@PC1test01]#catb.fa##测试dna序列CATCGTAATGACGGCCT[root@PC1test01]#cattest.py##计算程序#!/usr/bin/envpython......
  • 力扣---1448. 统计二叉树中好节点的数目
    给你一棵根为 root 的二叉树,请你返回二叉树中好节点的数目。「好节点」X定义为:从根到该节点X所经过的节点中,没有任何节点的值大于X的值。 示例1:输入:root=[3,1,4,3,null,1,5]输出:4解释:图中蓝色节点为好节点。根节点(3)永远是个好节点。节点4->(3,4)是路......