Skip to content

Commit e24a164

Browse files
committed
feat: add solutions to lc problem: No.1782
No.1782.Count Pairs Of Nodes
1 parent bbfed52 commit e24a164

File tree

6 files changed

+415
-2
lines changed

6 files changed

+415
-2
lines changed

solution/1700-1799/1782.Count Pairs Of Nodes/README.md

+148-1
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,169 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:哈希表 + 排序 + 二分查找**
57+
58+
根据题目,我们可以知道,与点对 $(a, b)$ 相连的边数等于“与 $a$ 相连的边数”加上“与 $b$ 相连的边数”,再减去同时与 $a$ 和 $b$ 相连的边数。
59+
60+
因此,我们可以先用数组 $cnt$ 统计与每个点相连的边数,用哈希表 $g$ 统计每个点对的数量。
61+
62+
然后,对于每个查询 $q$,我们可以枚举 $a$,对于每个 $a$,我们可以通过二分查找找到第一个满足 $cnt[a] + cnt[b] > q$ 的 $b$,先将数量累加到 $ans$ 中,再减去一部分重复的边数。
63+
64+
时间复杂度 $O(m\times n\times \log n)$。
65+
5666
<!-- tabs:start -->
5767

5868
### **Python3**
5969

6070
<!-- 这里可写当前语言的特殊实现逻辑 -->
6171

6272
```python
63-
73+
class Solution:
74+
def countPairs(self, n: int, edges: List[List[int]], queries: List[int]) -> List[int]:
75+
cnt = [0] * n
76+
g = defaultdict(int)
77+
for a, b in edges:
78+
a, b = a - 1, b - 1
79+
cnt[a] += 1
80+
cnt[b] += 1
81+
if a > b:
82+
a, b = b, a
83+
g[(a, b)] += 1
84+
85+
s = sorted(cnt)
86+
ans = [0] * len(queries)
87+
for i, t in enumerate(queries):
88+
for j, x in enumerate(s):
89+
k = bisect_right(s, t - x, lo=j+1)
90+
ans[i] += n - k
91+
for (a, b), v in g.items():
92+
if cnt[a] + cnt[b] > t and cnt[a] + cnt[b] - v <= t:
93+
ans[i] -= 1
94+
return ans
6495
```
6596

6697
### **Java**
6798

6899
<!-- 这里可写当前语言的特殊实现逻辑 -->
69100

70101
```java
102+
class Solution {
103+
public int[] countPairs(int n, int[][] edges, int[] queries) {
104+
int[] cnt = new int[n];
105+
Map<Integer, Integer> g = new HashMap<>();
106+
for (var e : edges) {
107+
int a = e[0] - 1, b = e[1] - 1;
108+
++cnt[a];
109+
++cnt[b];
110+
int k = Math.min(a, b) * n + Math.max(a, b);
111+
g.put(k, g.getOrDefault(k, 0) + 1);
112+
}
113+
int[] s = cnt.clone();
114+
Arrays.sort(s);
115+
int[] ans = new int[queries.length];
116+
for (int i = 0; i < queries.length; ++i) {
117+
int t = queries[i];
118+
for (int j = 0; j < n; ++j) {
119+
int x = s[j];
120+
int k = search(s, t - x, j + 1);
121+
ans[i] += n - k;
122+
}
123+
for (var e : g.entrySet()) {
124+
int a = e.getKey() / n, b = e.getKey() % n;
125+
int v = e.getValue();
126+
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
127+
--ans[i];
128+
}
129+
}
130+
}
131+
return ans;
132+
}
133+
134+
private int search(int[] arr, int x, int i) {
135+
int left = i, right = arr.length;
136+
while (left < right) {
137+
int mid = (left + right) >> 1;
138+
if (arr[mid] > x) {
139+
right = mid;
140+
} else {
141+
left = mid + 1;
142+
}
143+
}
144+
return left;
145+
}
146+
}
147+
```
148+
149+
### **C++**
150+
151+
```cpp
152+
class Solution {
153+
public:
154+
vector<int> countPairs(int n, vector<vector<int>>& edges, vector<int>& queries) {
155+
vector<int> cnt(n);
156+
unordered_map<int, int> g;
157+
for (auto& e : edges) {
158+
int a = e[0] - 1, b = e[1] - 1;
159+
++cnt[a];
160+
++cnt[b];
161+
int k = min(a, b) * n + max(a, b);
162+
++g[k];
163+
}
164+
vector<int> s = cnt;
165+
sort(s.begin(), s.end());
166+
vector<int> ans(queries.size());
167+
for (int i = 0; i < queries.size(); ++i) {
168+
int t = queries[i];
169+
for (int j = 0; j < n; ++j) {
170+
int x = s[j];
171+
int k = upper_bound(s.begin() + j + 1, s.end(), t - x) - s.begin();
172+
ans[i] += n - k;
173+
}
174+
for (auto& [k, v] : g) {
175+
int a = k / n, b = k % n;
176+
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
177+
--ans[i];
178+
}
179+
}
180+
}
181+
return ans;
182+
}
183+
};
184+
```
71185
186+
### **Go**
187+
188+
```go
189+
func countPairs(n int, edges [][]int, queries []int) []int {
190+
cnt := make([]int, n)
191+
g := map[int]int{}
192+
for _, e := range edges {
193+
a, b := e[0]-1, e[1]-1
194+
cnt[a]++
195+
cnt[b]++
196+
if a > b {
197+
a, b = b, a
198+
}
199+
g[a*n+b]++
200+
}
201+
s := make([]int, n)
202+
copy(s, cnt)
203+
sort.Ints(s)
204+
ans := make([]int, len(queries))
205+
for i, t := range queries {
206+
for j, x := range s {
207+
k := sort.Search(n, func(h int) bool { return s[h] > t-x && h > j })
208+
ans[i] += n - k
209+
}
210+
for k, v := range g {
211+
a, b := k/n, k%n
212+
if cnt[a]+cnt[b] > t && cnt[a]+cnt[b]-v <= t {
213+
ans[i]--
214+
}
215+
}
216+
}
217+
return ans
218+
}
72219
```
73220

74221
### **...**

solution/1700-1799/1782.Count Pairs Of Nodes/README_EN.md

+138-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,150 @@ The answers for each of the queries are as follows:
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def countPairs(self, n: int, edges: List[List[int]], queries: List[int]) -> List[int]:
62+
cnt = [0] * n
63+
g = defaultdict(int)
64+
for a, b in edges:
65+
a, b = a - 1, b - 1
66+
cnt[a] += 1
67+
cnt[b] += 1
68+
if a > b:
69+
a, b = b, a
70+
g[(a, b)] += 1
71+
72+
s = sorted(cnt)
73+
ans = [0] * len(queries)
74+
for i, t in enumerate(queries):
75+
for j, x in enumerate(s):
76+
k = bisect_right(s, t - x, lo=j+1)
77+
ans[i] += n - k
78+
for (a, b), v in g.items():
79+
if cnt[a] + cnt[b] > t and cnt[a] + cnt[b] - v <= t:
80+
ans[i] -= 1
81+
return ans
6182
```
6283

6384
### **Java**
6485

6586
```java
87+
class Solution {
88+
public int[] countPairs(int n, int[][] edges, int[] queries) {
89+
int[] cnt = new int[n];
90+
Map<Integer, Integer> g = new HashMap<>();
91+
for (var e : edges) {
92+
int a = e[0] - 1, b = e[1] - 1;
93+
++cnt[a];
94+
++cnt[b];
95+
int k = Math.min(a, b) * n + Math.max(a, b);
96+
g.put(k, g.getOrDefault(k, 0) + 1);
97+
}
98+
int[] s = cnt.clone();
99+
Arrays.sort(s);
100+
int[] ans = new int[queries.length];
101+
for (int i = 0; i < queries.length; ++i) {
102+
int t = queries[i];
103+
for (int j = 0; j < n; ++j) {
104+
int x = s[j];
105+
int k = search(s, t - x, j + 1);
106+
ans[i] += n - k;
107+
}
108+
for (var e : g.entrySet()) {
109+
int a = e.getKey() / n, b = e.getKey() % n;
110+
int v = e.getValue();
111+
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
112+
--ans[i];
113+
}
114+
}
115+
}
116+
return ans;
117+
}
118+
119+
private int search(int[] arr, int x, int i) {
120+
int left = i, right = arr.length;
121+
while (left < right) {
122+
int mid = (left + right) >> 1;
123+
if (arr[mid] > x) {
124+
right = mid;
125+
} else {
126+
left = mid + 1;
127+
}
128+
}
129+
return left;
130+
}
131+
}
132+
```
133+
134+
### **C++**
135+
136+
```cpp
137+
class Solution {
138+
public:
139+
vector<int> countPairs(int n, vector<vector<int>>& edges, vector<int>& queries) {
140+
vector<int> cnt(n);
141+
unordered_map<int, int> g;
142+
for (auto& e : edges) {
143+
int a = e[0] - 1, b = e[1] - 1;
144+
++cnt[a];
145+
++cnt[b];
146+
int k = min(a, b) * n + max(a, b);
147+
++g[k];
148+
}
149+
vector<int> s = cnt;
150+
sort(s.begin(), s.end());
151+
vector<int> ans(queries.size());
152+
for (int i = 0; i < queries.size(); ++i) {
153+
int t = queries[i];
154+
for (int j = 0; j < n; ++j) {
155+
int x = s[j];
156+
int k = upper_bound(s.begin() + j + 1, s.end(), t - x) - s.begin();
157+
ans[i] += n - k;
158+
}
159+
for (auto& [k, v] : g) {
160+
int a = k / n, b = k % n;
161+
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
162+
--ans[i];
163+
}
164+
}
165+
}
166+
return ans;
167+
}
168+
};
169+
```
66170
171+
### **Go**
172+
173+
```go
174+
func countPairs(n int, edges [][]int, queries []int) []int {
175+
cnt := make([]int, n)
176+
g := map[int]int{}
177+
for _, e := range edges {
178+
a, b := e[0]-1, e[1]-1
179+
cnt[a]++
180+
cnt[b]++
181+
if a > b {
182+
a, b = b, a
183+
}
184+
g[a*n+b]++
185+
}
186+
s := make([]int, n)
187+
copy(s, cnt)
188+
sort.Ints(s)
189+
ans := make([]int, len(queries))
190+
for i, t := range queries {
191+
for j, x := range s {
192+
k := sort.Search(n, func(h int) bool { return s[h] > t-x && h > j })
193+
ans[i] += n - k
194+
}
195+
for k, v := range g {
196+
a, b := k/n, k%n
197+
if cnt[a]+cnt[b] > t && cnt[a]+cnt[b]-v <= t {
198+
ans[i]--
199+
}
200+
}
201+
}
202+
return ans
203+
}
67204
```
68205

69206
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
vector<int> countPairs(int n, vector<vector<int>>& edges, vector<int>& queries) {
4+
vector<int> cnt(n);
5+
unordered_map<int, int> g;
6+
for (auto& e : edges) {
7+
int a = e[0] - 1, b = e[1] - 1;
8+
++cnt[a];
9+
++cnt[b];
10+
int k = min(a, b) * n + max(a, b);
11+
++g[k];
12+
}
13+
vector<int> s = cnt;
14+
sort(s.begin(), s.end());
15+
vector<int> ans(queries.size());
16+
for (int i = 0; i < queries.size(); ++i) {
17+
int t = queries[i];
18+
for (int j = 0; j < n; ++j) {
19+
int x = s[j];
20+
int k = upper_bound(s.begin() + j + 1, s.end(), t - x) - s.begin();
21+
ans[i] += n - k;
22+
}
23+
for (auto& [k, v] : g) {
24+
int a = k / n, b = k % n;
25+
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
26+
--ans[i];
27+
}
28+
}
29+
}
30+
return ans;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
func countPairs(n int, edges [][]int, queries []int) []int {
2+
cnt := make([]int, n)
3+
g := map[int]int{}
4+
for _, e := range edges {
5+
a, b := e[0]-1, e[1]-1
6+
cnt[a]++
7+
cnt[b]++
8+
if a > b {
9+
a, b = b, a
10+
}
11+
g[a*n+b]++
12+
}
13+
s := make([]int, n)
14+
copy(s, cnt)
15+
sort.Ints(s)
16+
ans := make([]int, len(queries))
17+
for i, t := range queries {
18+
for j, x := range s {
19+
k := sort.Search(n, func(h int) bool { return s[h] > t-x && h > j })
20+
ans[i] += n - k
21+
}
22+
for k, v := range g {
23+
a, b := k/n, k%n
24+
if cnt[a]+cnt[b] > t && cnt[a]+cnt[b]-v <= t {
25+
ans[i]--
26+
}
27+
}
28+
}
29+
return ans
30+
}

0 commit comments

Comments
 (0)