当前位置:网站首页>1782. 统计点对的数目 双指针
1782. 统计点对的数目 双指针
2022-07-31 01:01:00 【钰娘娘】
1782. 统计点对的数目
给你一个无向图,无向图由整数
n,表示图中节点的数目,和edges组成,其中edges[i] = [ui, vi]表示ui和vi之间有一条无向边。同时给你一个代表查询的整数数组queries。第
j个查询的答案是满足如下条件的点对(a, b)的数目:
a < bcnt是与a或者b相连的边的数目,且cnt严格大于queries[j]。请你返回一个数组
answers,其中answers.length == queries.length且answers[j]是第j个查询的答案。请注意,图中可能会有 重复边 。
示例 1:
输入:n = 4, edges = [[1,2],[2,4],[1,3],[2,3],[2,1]], queries = [2,3] 输出:[6,5] 解释:每个点对中,与至少一个点相连的边的数目如上图所示。示例 2:
输入:n = 5, edges = [[1,5],[1,5],[3,4],[2,5],[1,3],[5,1],[2,3],[2,5]], queries = [1,2,3,4,5] 输出:[10,10,9,8,6]提示:
2 <= n <= 2 * 1e41 <= edges.length <= 1e51 <= ui, vi <= nui != vi1 <= queries.length <= 200 <= queries[j] < edges.length来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/count-pairs-of-nodes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
L
不会写,看了别人的思路。这题又属于复杂度卡的题,参考:度+遍历边
方法:双指针
即便看了思路,还是好难写
1,统计 from->(to,cnt) 每个点到另一个点对应的边数目
2. 统计所有点入度数目
3. 对所有数目排序
4. 双指针,定一点移动令一点,中间的为以一端为起点的可能的点对数目
5. 检查所有边(注意去重),如果原本加起来符合,减掉连线后的值不符合则从答案中移除
class Solution {
public int[] countPairs(int n, int[][] edges, int[] queries) {
Map<Integer,Map<Integer,Integer>>map = new HashMap<>(); //from->(to,cnt)
int[] cnts = new int[n+1];
for(int[] edge:edges){
int a = edge[0];
int b = edge[1];
map.putIfAbsent(a,new HashMap<>());
map.putIfAbsent(b,new HashMap<>());
map.get(a).put(b,map.get(a).getOrDefault(b,0)+1);
map.get(b).put(a,map.get(b).getOrDefault(a,0)+1);
cnts[a]++;
cnts[b]++;
}
int[] singles =Arrays.copyOf(cnts,cnts.length);
Arrays.sort(singles);
int len = queries.length;
int[] ans = new int[len];
for(int i = 0; i < len; i++){
int left = 1;
int right = n;
while(left<right){
while (left<right&&singles[left]+singles[right]<=queries[i]) ++left;
if(left<right){
ans[i]+=right-left;
}
--right;
}
Set<Long> set = new HashSet<>();
for(int []edge:edges){
int a = Math.min(edge[0],edge[1]);
int b = Math.max(edge[0],edge[1]);
long check = (long)a*(n+1)+b;
if(set.contains(check)) continue;
set.add(check);
int pre = cnts[a]+cnts[b];
int v= pre-map.getOrDefault(a,new HashMap<>()).getOrDefault(b,0);
if(pre>queries[i] && v<=queries[i])
ans[i] -= 1;
}
}
return ans;
}
}边栏推荐
- 图像处理工具设计
- 金融政企被攻击为什么要用高防CDN?
- MySQL高级-六索引优化
- TypeScript在使用中出现的问题记录
- A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES
- BOM系列之Navigator对象
- 297. 二叉树的序列化与反序列化
- 无线模块的参数介绍和选型要点
- Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
- 射频器件的基本参数1
猜你喜欢

ShardingSphere read-write separation (8)

ShardingSphere's vertical sub-database sub-table actual combat (5)

4G通信模块CAT1和CAT4的区别

Error occurred while trying to proxy request The project suddenly can't get up

系统设计.短链系统设计

《实战》基于电商领域的词性提取及其决策树模型建模

API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试

一万了解 Gateway 知识点

DOM系列之scroll系列

Rocky/GNU之Zabbix部署(1)
随机推荐
822. 走方格
typescript15- (specify both parameter and return value types)
【Demo】ABAP Base64加解密测试
小黑leetcode之旅:104. 二叉树的最大深度
MySQL notes under
Oracle has a weird temporary table space shortage problem
权限管理怎么做的?
《实战》基于电商领域的词性提取及其决策树模型建模
Detailed explanation of 9 common reasons for MySQL index failure
A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES
埃拉托斯特尼筛法
这个项目太有极客范儿了
Can deep learning solve the parameters of a specific function?
解析云原生消息流系统 Apache Pulsar 能力及场景
typescript9-常用基础类型
华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
Yolov7实战,实现网页端的实时目标检测
GO GOPROXY proxy Settings
typescript10-commonly used basic types
分布式.分布式锁
