• 力扣第290场周赛T3T4


    t3.统计包含每个点的矩阵数目

    题意

    给定一些矩阵和一些点,计算每个点被多少个矩阵包含着

    思路

    题目中矩阵的高度范围是1~100,因此,将每个高度对应的矩阵宽度存下来,排序。枚举每个点,每个点枚举高度比它大的,然后在每个高度中二分宽度,找到第一个大于点的宽度。
    找到每个高度有多少个矩阵可以包含点,每个高度得到的结果相加,就是该点被包含的矩阵的数量。

    代码

    class Solution {
    public:
        vector<int> countRectangles(vector<vector<int>>& rectangles, vector<vector<int>>& points) {
            
            vector<int> h[110];
            for(auto re : rectangles)
                h[re[1]].push_back(re[0]);
            for(int i = 1; i <= 100; i ++ )
                sort(h[i].begin(), h[i].end());
            
            vector<int> ans;
            
            for(auto point : points)
            {
                int x = point[0], y = point[1];
                int cnt = 0;
                for(int i = y; i <= 100; i ++ )
                {
                    if(h[i].size() == 0)    continue;
                    int l = 0, r = h[i].size() - 1;
                    while(l < r)
                    {
                        int mid = l + r >> 1;
                        if(h[i][mid] >= x)  r = mid;
                        else    l = mid + 1;
                    }
                    if(h[i][l] >= x)    cnt += h[i].size() - 1 - l + 1;
                }
                ans.push_back(cnt);
            }
            return ans;
        }
    };
    

    t4.花期内花的数目

    题意

    每朵花有一段花期,每个人在一个时间点看花,能看到几朵花。

    思路

    根据差分的思想,可以在花期[start, end],在start处加一,在end+1处减一,在人的时间点上,计算在该时间之前有多少花已经开了,有多少花已经谢了。

    代码

    class Solution {
    public:
        vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& persons) {
            vector<pair<int, int>> a;
            vector<int> ans(persons.size());
    
            for(auto f : flowers)
            {
                a.push_back({f[0], 1});   // 花期开始+1
                a.push_back({f[1] + 1, -1});  // 花期结束-1
            }
            for(int i = 0; i < persons.size(); i ++ )
                a.push_back({persons[i], i + 50010}); 
            sort(a.begin(), a.end());
    
            int cnt = 0;
            for(auto p : a)
            {
                int x = p.first, y = p.second;
                if(y != 1 && y != -1)   // 查询
                    ans[y - 50010] = cnt;
                else    cnt += y;
            }
            return ans;
        }
    };
    
  • 相关阅读:
    nginx安装
    Mybatis使用generator自动生成映射配置文件信息
    Mysql报错Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
    JS获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
    js获取select标签选中的值
    linux下使用ffmpeg将amr转成mp3
    String与InputStream互转的几种方法
    javascript,检测对象中是否存在某个属性
    SQL语句在查询分析器中可以执行,代码中不能执行
    shell实现SSH自动登陆
  • 原文地址:https://www.cnblogs.com/Hfolsvh/p/16185205.html
Copyright © 2020-2023  润新知