[Lc_week] 447 | 155 | Q1 | hash | pair {}调用

447_Q1

题解

复制代码
class Solution {
    typedef pair<int,int> PII;

    // 自定义哈希函数
    struct HashPII {
        size_t operator()(const PII& p) const {
            return hash<int>()(p.first) ^ (hash<int>()(p.second) << 1);
        }
    };

public:
    int countCoveredBuildings(int n, vector<vector<int>>& buildings) 
    {
         //不管有多远 至少存在一个建筑
        //bfs变形

        //哦不对,hash 快速查找同行同列,不就可以判断了吗

//添加自定义哈希函数HashPII,使用unordered_map<PII, bool, HashPII>解决pair作为键的问题
        unordered_map<PII,bool,HashPII> hash;
        int ans=0;
        for(auto& b:buildings)
            {
                PII p={b[0],b[1]};  //pair构造和调用 要用{}
                hash[p]=true;
            }
        for(auto& b:buildings)
            {
                int cnt=0;
                //四个 方向的查找
                //上
                for(int i=b[1]+1;i<=n;i++)
                    {
                        if(hash.count({b[0],i}))
                        {
                            cnt++;
                            break;
                        }
                    }
                for(int i=b[1]-1;i>=1;i--)
                    {
                        if(hash.count({b[0],i}))
                        {
                            cnt++;
                            break;
                        }
                    }
                for(int i=b[0]-1;i>=1;i--)
                    {
                        if(hash.count({i,b[1]}))
                        {
                            cnt++;
                            break;
                        }
                    }
                for(int i=b[0]+1;i<=n;i++)
                    {
                        if(hash.count({i,b[1]}))
                        {
                            cnt++;
                            break;
                        }
                    }
                if(cnt==4) ans++;
            }
        return ans;
    }
};©leetcode
  • pair 构造和调用 要用 {}
  • !!!!!!!!!!!!!!花括号

超时了

优化

  • 采取 行列极值法

    class Solution {
    public:
    int countCoveredBuildings(int n, vector<vector<int>>& buildings) {
    // 存 行位置极值和列位置极值
    unordered_map<int, int> row_min, row_max;
    unordered_map<int, int> col_min, col_max;

    复制代码
          for (auto& b : buildings) 
          {
              int x = b[0], y = b[1];
    
              // 预处理行极值
              if (!row_min.count(x) || y < row_min[x])
                  row_min[x] = y;
              if (!row_max.count(x) || y > row_max[x])
                  row_max[x] = y;
    
              // 预处理列极值
              if (!col_min.count(y) || x < col_min[y])
                  col_min[y] = x;
              if (!col_max.count(y) || x > col_max[y])
                  col_max[y] = x;
          }
          int ans = 0;
          for (auto& b : buildings) 
          {
              int x = b[0], y = b[1];
              bool up = (y < row_max[x]);
              bool down = (y > row_min[x]);
              bool left = (x > col_min[y]);
              bool right = (x < col_max[y]);
              if (up && down && left && right)
                  ans++;
          }
          return ans;
      }

    };©leetcode

155_Q1

复制代码
class Solution {
public:
    string findCommonResponse(vector<vector<string>>& responses) 
    {
        unordered_map<string,int> hash;
        for(auto& re:responses)
            {
                unordered_map<string,bool> check;
                for(auto& str:re)
                    {
                        if(!check[str])
                        {
                             check[str]=true;
                            hash[str]++; //确保 每一个 只加一次
                        }
                    }
            }
        pair<string,int> ret(responses[0][0],0);
        for(auto& [a,b]:hash)
            {
                string& f=ret.first;
                int& g=ret.second;
                if(b==g)
                {
                    if(a<f)
                        f=a;
                }
                if(b>g)
                {
                    f=a;
                    g=b;
                }
            }
        return ret.first;
    }
};
相关推荐
bkspiderx1 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
中华小当家呐2 小时前
算法之常见八大排序
数据结构·算法·排序算法
沐怡旸3 小时前
【算法--链表】114.二叉树展开为链表--通俗讲解
算法·面试
一只懒洋洋3 小时前
K-meas 聚类、KNN算法、决策树、随机森林
算法·决策树·聚类
方案开发PCBA抄板芯片解密4 小时前
什么是算法:高效解决问题的逻辑框架
算法
songx_995 小时前
leetcode9(跳跃游戏)
数据结构·算法·游戏
小白狮ww5 小时前
RStudio 教程:以抑郁量表测评数据分析为例
人工智能·算法·机器学习
AAA修煤气灶刘哥5 小时前
接口又被冲崩了?Sentinel 这 4 种限流算法,帮你守住后端『流量安全阀』
后端·算法·spring cloud
kk”6 小时前
C语言快速排序
数据结构·算法·排序算法
纪元A梦6 小时前
贪心算法应用:基因编辑靶点选择问题详解
算法·贪心算法