当前位置:网站首页>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;
}
}边栏推荐
- 一万了解 Gateway 知识点
- Rocky/GNU之Zabbix部署(1)
- A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES
- TiDB 在多点数字化零售场景下的应用
- [Yugong Series] July 2022 Go Teaching Course 016-Logical Operators and Other Operators of Operators
- ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!
- typescript13-类型别名
- typescript17-函数可选参数
- ROS2系列知识(3):环境配置
- 解析云原生消息流系统 Apache Pulsar 能力及场景
猜你喜欢

What is Promise?What is the principle of Promise?How to use Promises?

VS warning LNK4099:未找到 PDB 的解决方案

网站频繁出现mysql等数据库连接失败等信息解决办法

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

【952. Calculate the maximum component size according to the common factor】

ShardingSphere之垂直分库分表实战(五)

仿牛客网项目总结

The level of ShardingSphere depots in actual combat (4)

Responsive layout vs px/em/rem

这个项目太有极客范儿了
随机推荐
MySql data recovery method personal summary
ShardingSphere之未分片表配置实战(六)
Xiaohei's leetcode journey: 117. Fill the next right node pointer of each node II
无线模块的参数介绍和选型要点
typescript18-对象类型
程序员工作三年攒多少钱合适?
Mysql: Invalid default value for TIMESTAMP
Niuke.com question brushing training (4)
TiCDC 架构和数据同步链路解析
Adding, deleting, modifying and checking the foundation of MySQL
蓝牙mesh系统开发三 Ble Mesh 配网器 Provisioner
Unity2D horizontal version game tutorial 4 - item collection and physical materials
typescript11-数据类型
ABC 261 F - Sorting Color Balls (reverse pair)
822. 走方格
分布式.幂等性
ECCV 2022丨轻量级模型架构火了,力压苹果MobileViT(附代码和论文下载)
ros2知识:在单个进程中布置多个节点
不用Swagger,那我用啥?
ShardingSphere's unsharded table configuration combat (6)
