ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

0702训练题目题解

2022-07-03 13:02:51  阅读:104  来源: 互联网

标签:题目 HashMap idx int 题解 ++ vector bombs 0702


[LC2300]

class Solution {
public:
    using LL = long long;
    vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
        //sort(spells.begin(), spells.end());
        sort(potions.begin(), potions.end());

        int n = spells.size(), m = potions.size();
        int r = m - 1;
        vector<int> ans(n , 0);
        for (int i = 0; i < n; i++) {
            int t = getVal(spells[i], potions, success); //返回满足第i个咒语组合药水的数目;
            ans[i] = t;
        }
        return ans;
    }

    int getVal(int val, vector<int>& potions, LL success)
    {
        int n = potions.size();
        int l = 0, r = n - 1;
        while (l < r) {
            int mid = (l + r) / 2;
            if ((LL)val * potions[mid] >= success) r = mid;
            else l = mid + 1;
        }

        //判断边界情况;
        if ((LL)val * potions[l] >= success)
            return n - l;
        return 0;
    }
};

[LC2101]

//先建图,然后DFS/BFS;
//图上总共点数不超过100,边数最多100*100;
const int N = 110, M = 10010;
int h[N], e[M], ne[M], idx; //不用带边权;
int cnt[N];
//考虑存在自环的情况;
void add(int a, int b)
{
    //cnt[a] = 1, cnt[b] = 1;
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

//因为链式前向星是单个id进行建图,所以需要将二维坐标映射为1维坐标;

class Solution {
public:
    using PII = pair<int, int>;
    using LL = long long;
    void init()
    {
        memset(h, -1, sizeof h);
        idx = 0;
        //memset(cnt, 0, sizeof cnt);
    }

    unordered_map<LL, int> HashMap;
    int id = 0;
    int ans = 0;
    unordered_map<int, int> CntHash;
    int maximumDetonation(vector<vector<int>>& bombs) {
        init();
        int n = bombs.size();
        //满足爆炸范围的进行建图;
        for (int i = 0; i < n; i++)
        {
            LL p1 = bombs[i][0] * 1e5 + bombs[i][1]; 
            if (!HashMap.count(p1)) {
                HashMap[p1] = ++id;
            }
            int a = HashMap[p1];
            CntHash[a]++;
            int r = bombs[i][2]; //半径;
            for (int j = 0; j < n; j++) {
                if (i != j) {
                    LL p2 = bombs[j][0]*1e5 + bombs[j][1];
                    if (!HashMap.count(p2)) {
                        HashMap[p2] = ++id;
                    }
                    int b = HashMap[p2];
                    LL deltax = abs(bombs[i][0] - bombs[j][0]);
                    LL deltay = abs(bombs[i][1] - bombs[j][1]);
                    LL dis = deltax*deltax + deltay*deltay;
                    if (dis <= (LL)r*r) {
                        if (a != b)
                            add(a, b);
                        else 
                        {
                            cnt[a]++;
                            cnt[b]++;
                        }
                    }
                }
            }
        }

        //因为爆炸传递存在有向性,所以需要枚举所有的爆炸起点;

        for (int i = 0; i < n; i++)
        {
            LL p1 = bombs[i][0] * 1e5 + bombs[i][1]; 
            unordered_map<int, int> temp; //已经爆炸过的节点;
            ans = max(ans, dfs(HashMap[p1], temp));
        }

        return ans;
    }

    int dfs(int u, unordered_map<int, int>& hashMap0)
    {
        //当前节点爆炸;
        hashMap0[u]++;
        int res = 0;
        res += CntHash[u];
        for (int i = h[u]; ~i; i = ne[i]) {
            int j = e[i];
            if (!hashMap0.count(j))
                res += dfs(j, hashMap0);
        }
        return res;
    }

};

[LC2115]

const int N = 1e4 + 10;
int h[N], e[N], ne[N], idx;
int din[N], dout[N]; //出度入度;

void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
    din[b]++, dout[a]++;
}

class Solution {
public:
    void init()
    {
        memset(h, -1, sizeof h);
        memset(din, 0, sizeof din);
        memset(dout, 0, sizeof dout);
        idx = 0;
    }

    unordered_map<string, int> HashMap; //将字符串映射成整数;
    unordered_map<int, string> unHashMap;
    int StrIdx = 0;
    unordered_map<string, int> recipesMap;
    //unordered_map<int, int> recipesInts;
    vector<string> ans;

    vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
        //构建树;
        init();
        //反向建图
        for (int i = 0; i < recipes.size(); i++)
        {
            if (!HashMap.count(recipes[i])) { 
                HashMap[recipes[i]] = ++StrIdx;
                unHashMap[StrIdx] = recipes[i];
            }
            int a = HashMap[recipes[i]];
            for (int j = 0; j < ingredients[i].size(); j++) {
                if (!HashMap[ingredients[i][j]]) {
                     HashMap[ingredients[i][j]] = ++StrIdx;
                     unHashMap[StrIdx] = ingredients[i][j];
                }
                int b = HashMap[ingredients[i][j]];
                add(b, a); //从b->a连一条边;
            }
        }

        for (auto s : recipes) recipesMap[s]++;
        // for (auto it = HashMap.begin(); it != HashMap.end(); ++it)
        //     cout << it->first << endl;

        queue<int> qu;
        for (auto s : supplies)  {
            //cout << s << "->" ;
            if (HashMap.count(s)) {
                int id = HashMap[s];
                //cout << id <<" :" << din[id] << endl; 
                if (!din[id]) qu.push(id); //入度为0的节点入队;
            }
        }

        //cout << qu.size() << endl;
        while (qu.size()) {
            int u = qu.front();
            qu.pop();
            for (int i = h[u]; ~i; i = ne[i]) {
                int j = e[i]; //
                if (--din[j] == 0) { //说明入度为0且是道菜,说明可以被做出来;
                    qu.push(j);
                    if (recipesMap.count(unHashMap[j])) 
                        ans.push_back(unHashMap[j]);
                }
            }
        }
        return ans;
    }
};

标签:题目,HashMap,idx,int,题解,++,vector,bombs,0702
来源: https://www.cnblogs.com/zhanghanLeo/p/16439576.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有