当前位置:网站首页>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;
}
}边栏推荐
- ShardingSphere's public table combat (7)
- MySql data recovery method personal summary
- VS warning LNK4099:未找到 PDB 的解决方案
- Error occurred while trying to proxy request The project suddenly can't get up
- 金融政企被攻击为什么要用高防CDN?
- Artificial Intelligence and Cloud Security
- MySQL master-slave replication and read-write separation script - pro test available
- ShardingSphere之未分片表配置实战(六)
- TiDB 在多点数字化零售场景下的应用
- Preparations for web vulnerabilities
猜你喜欢
随机推荐
Redis learning
埃拉托斯特尼筛法
Installation problem corresponding to tensorflow and GPU version
GO GOPROXY proxy Settings
场景之多数据源查询及数据下载问题
ShardingSphere之水平分库实战(四)
ShardingSphere之读写分离(八)
小黑leetcode之旅:117. 填充每个节点的下一个右侧节点指针 II
typescript10-常用基础类型
TiCDC 架构和数据同步链路解析
【ABAP】MFBF过账到质量检验库存类型Demo
WMware Tools installation failed segmentation fault solution
The level of ShardingSphere depots in actual combat (4)
Summary of MySQL database interview questions (2022 latest version)
Rocky/GNU之Zabbix部署(2)
typescript17-函数可选参数
Sping.事务的传播特性
深度学习可以求解特定函数的参数么?
TiDB 操作实践 -- 备份与恢复
Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu









