Skip to content

Commit f87226f

Browse files
authored
feat: add solutions to lc problem: No.1782 (doocs#1488)
No.1782.Count Pairs Of Nodes
1 parent 0c70b93 commit f87226f

File tree

5 files changed

+203
-76
lines changed

5 files changed

+203
-76
lines changed

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

+49-5
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959

6060
因此,我们可以先用数组 $cnt$ 统计与每个点相连的边数,用哈希表 $g$ 统计每个点对的数量。
6161

62-
然后,对于每个查询 $q$,我们可以枚举 $a$,对于每个 $a$,我们可以通过二分查找找到第一个满足 $cnt[a] + cnt[b] > q$ 的 $b$,先将数量累加到 $ans$ 中,再减去一部分重复的边数
62+
然后,对于每个查询 $q$,我们可以枚举 $a$,对于每个 $a$,我们可以通过二分查找找到第一个满足 $cnt[a] + cnt[b] > q$ 的 $b$,先将数量累加到当前查询的答案中,然后减去一部分重复的边
6363

64-
时间复杂度 $O(m\times n\times \log n)$
64+
时间复杂度 $O(q \times (n \times \log n + m))$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是点数和边数,而 $q$ 是查询数
6565

6666
<!-- tabs:start -->
6767

@@ -78,10 +78,9 @@ class Solution:
7878
g = defaultdict(int)
7979
for a, b in edges:
8080
a, b = a - 1, b - 1
81+
a, b = min(a, b), max(a, b)
8182
cnt[a] += 1
8283
cnt[b] += 1
83-
if a > b:
84-
a, b = b, a
8584
g[(a, b)] += 1
8685

8786
s = sorted(cnt)
@@ -110,7 +109,7 @@ class Solution {
110109
++cnt[a];
111110
++cnt[b];
112111
int k = Math.min(a, b) * n + Math.max(a, b);
113-
g.put(k, g.getOrDefault(k, 0) + 1);
112+
g.merge(k, 1, Integer::sum);
114113
}
115114
int[] s = cnt.clone();
116115
Arrays.sort(s);
@@ -220,6 +219,51 @@ func countPairs(n int, edges [][]int, queries []int) []int {
220219
}
221220
```
222221

222+
### **TypeScript**
223+
224+
```ts
225+
function countPairs(n: number, edges: number[][], queries: number[]): number[] {
226+
const cnt: number[] = new Array(n).fill(0);
227+
const g: Map<number, number> = new Map();
228+
for (const [a, b] of edges) {
229+
++cnt[a - 1];
230+
++cnt[b - 1];
231+
const k = Math.min(a - 1, b - 1) * n + Math.max(a - 1, b - 1);
232+
g.set(k, (g.get(k) || 0) + 1);
233+
}
234+
const s = cnt.slice().sort((a, b) => a - b);
235+
const search = (nums: number[], x: number, l: number): number => {
236+
let r = nums.length;
237+
while (l < r) {
238+
const mid = (l + r) >> 1;
239+
if (nums[mid] > x) {
240+
r = mid;
241+
} else {
242+
l = mid + 1;
243+
}
244+
}
245+
return l;
246+
};
247+
const ans: number[] = [];
248+
for (const t of queries) {
249+
let res = 0;
250+
for (let j = 0; j < s.length; ++j) {
251+
const k = search(s, t - s[j], j + 1);
252+
res += n - k;
253+
}
254+
for (const [k, v] of g) {
255+
const a = Math.floor(k / n);
256+
const b = k % n;
257+
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
258+
--res;
259+
}
260+
}
261+
ans.push(res);
262+
}
263+
return ans;
264+
}
265+
```
266+
223267
### **...**
224268

225269
```

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

+47-3
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ class Solution:
6565
g = defaultdict(int)
6666
for a, b in edges:
6767
a, b = a - 1, b - 1
68+
a, b = min(a, b), max(a, b)
6869
cnt[a] += 1
6970
cnt[b] += 1
70-
if a > b:
71-
a, b = b, a
7271
g[(a, b)] += 1
7372

7473
s = sorted(cnt)
@@ -95,7 +94,7 @@ class Solution {
9594
++cnt[a];
9695
++cnt[b];
9796
int k = Math.min(a, b) * n + Math.max(a, b);
98-
g.put(k, g.getOrDefault(k, 0) + 1);
97+
g.merge(k, 1, Integer::sum);
9998
}
10099
int[] s = cnt.clone();
101100
Arrays.sort(s);
@@ -205,6 +204,51 @@ func countPairs(n int, edges [][]int, queries []int) []int {
205204
}
206205
```
207206

207+
### **TypeScript**
208+
209+
```ts
210+
function countPairs(n: number, edges: number[][], queries: number[]): number[] {
211+
const cnt: number[] = new Array(n).fill(0);
212+
const g: Map<number, number> = new Map();
213+
for (const [a, b] of edges) {
214+
++cnt[a - 1];
215+
++cnt[b - 1];
216+
const k = Math.min(a - 1, b - 1) * n + Math.max(a - 1, b - 1);
217+
g.set(k, (g.get(k) || 0) + 1);
218+
}
219+
const s = cnt.slice().sort((a, b) => a - b);
220+
const search = (nums: number[], x: number, l: number): number => {
221+
let r = nums.length;
222+
while (l < r) {
223+
const mid = (l + r) >> 1;
224+
if (nums[mid] > x) {
225+
r = mid;
226+
} else {
227+
l = mid + 1;
228+
}
229+
}
230+
return l;
231+
};
232+
const ans: number[] = [];
233+
for (const t of queries) {
234+
let res = 0;
235+
for (let j = 0; j < s.length; ++j) {
236+
const k = search(s, t - s[j], j + 1);
237+
res += n - k;
238+
}
239+
for (const [k, v] of g) {
240+
const a = Math.floor(k / n);
241+
const b = k % n;
242+
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
243+
--res;
244+
}
245+
}
246+
ans.push(res);
247+
}
248+
return ans;
249+
}
250+
```
251+
208252
### **...**
209253

210254
```
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
class Solution {
2-
public int[] countPairs(int n, int[][] edges, int[] queries) {
3-
int[] cnt = new int[n];
4-
Map<Integer, Integer> g = new HashMap<>();
5-
for (var e : edges) {
6-
int a = e[0] - 1, b = e[1] - 1;
7-
++cnt[a];
8-
++cnt[b];
9-
int k = Math.min(a, b) * n + Math.max(a, b);
10-
g.put(k, g.getOrDefault(k, 0) + 1);
11-
}
12-
int[] s = cnt.clone();
13-
Arrays.sort(s);
14-
int[] ans = new int[queries.length];
15-
for (int i = 0; i < queries.length; ++i) {
16-
int t = queries[i];
17-
for (int j = 0; j < n; ++j) {
18-
int x = s[j];
19-
int k = search(s, t - x, j + 1);
20-
ans[i] += n - k;
21-
}
22-
for (var e : g.entrySet()) {
23-
int a = e.getKey() / n, b = e.getKey() % n;
24-
int v = e.getValue();
25-
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
26-
--ans[i];
27-
}
28-
}
29-
}
30-
return ans;
31-
}
32-
33-
private int search(int[] arr, int x, int i) {
34-
int left = i, right = arr.length;
35-
while (left < right) {
36-
int mid = (left + right) >> 1;
37-
if (arr[mid] > x) {
38-
right = mid;
39-
} else {
40-
left = mid + 1;
41-
}
42-
}
43-
return left;
44-
}
1+
class Solution {
2+
public int[] countPairs(int n, int[][] edges, int[] queries) {
3+
int[] cnt = new int[n];
4+
Map<Integer, Integer> g = new HashMap<>();
5+
for (var e : edges) {
6+
int a = e[0] - 1, b = e[1] - 1;
7+
++cnt[a];
8+
++cnt[b];
9+
int k = Math.min(a, b) * n + Math.max(a, b);
10+
g.merge(k, 1, Integer::sum);
11+
}
12+
int[] s = cnt.clone();
13+
Arrays.sort(s);
14+
int[] ans = new int[queries.length];
15+
for (int i = 0; i < queries.length; ++i) {
16+
int t = queries[i];
17+
for (int j = 0; j < n; ++j) {
18+
int x = s[j];
19+
int k = search(s, t - x, j + 1);
20+
ans[i] += n - k;
21+
}
22+
for (var e : g.entrySet()) {
23+
int a = e.getKey() / n, b = e.getKey() % n;
24+
int v = e.getValue();
25+
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
26+
--ans[i];
27+
}
28+
}
29+
}
30+
return ans;
31+
}
32+
33+
private int search(int[] arr, int x, int i) {
34+
int left = i, right = arr.length;
35+
while (left < right) {
36+
int mid = (left + right) >> 1;
37+
if (arr[mid] > x) {
38+
right = mid;
39+
} else {
40+
left = mid + 1;
41+
}
42+
}
43+
return left;
44+
}
4545
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
class Solution:
2-
def countPairs(
3-
self, n: int, edges: List[List[int]], queries: List[int]
4-
) -> List[int]:
5-
cnt = [0] * n
6-
g = defaultdict(int)
7-
for a, b in edges:
8-
a, b = a - 1, b - 1
9-
cnt[a] += 1
10-
cnt[b] += 1
11-
if a > b:
12-
a, b = b, a
13-
g[(a, b)] += 1
14-
15-
s = sorted(cnt)
16-
ans = [0] * len(queries)
17-
for i, t in enumerate(queries):
18-
for j, x in enumerate(s):
19-
k = bisect_right(s, t - x, lo=j + 1)
20-
ans[i] += n - k
21-
for (a, b), v in g.items():
22-
if cnt[a] + cnt[b] > t and cnt[a] + cnt[b] - v <= t:
23-
ans[i] -= 1
24-
return ans
1+
class Solution:
2+
def countPairs(
3+
self, n: int, edges: List[List[int]], queries: List[int]
4+
) -> List[int]:
5+
cnt = [0] * n
6+
g = defaultdict(int)
7+
for a, b in edges:
8+
a, b = a - 1, b - 1
9+
a, b = min(a, b), max(a, b)
10+
cnt[a] += 1
11+
cnt[b] += 1
12+
g[(a, b)] += 1
13+
14+
s = sorted(cnt)
15+
ans = [0] * len(queries)
16+
for i, t in enumerate(queries):
17+
for j, x in enumerate(s):
18+
k = bisect_right(s, t - x, lo=j + 1)
19+
ans[i] += n - k
20+
for (a, b), v in g.items():
21+
if cnt[a] + cnt[b] > t and cnt[a] + cnt[b] - v <= t:
22+
ans[i] -= 1
23+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function countPairs(n: number, edges: number[][], queries: number[]): number[] {
2+
const cnt: number[] = new Array(n).fill(0);
3+
const g: Map<number, number> = new Map();
4+
for (const [a, b] of edges) {
5+
++cnt[a - 1];
6+
++cnt[b - 1];
7+
const k = Math.min(a - 1, b - 1) * n + Math.max(a - 1, b - 1);
8+
g.set(k, (g.get(k) || 0) + 1);
9+
}
10+
const s = cnt.slice().sort((a, b) => a - b);
11+
const search = (nums: number[], x: number, l: number): number => {
12+
let r = nums.length;
13+
while (l < r) {
14+
const mid = (l + r) >> 1;
15+
if (nums[mid] > x) {
16+
r = mid;
17+
} else {
18+
l = mid + 1;
19+
}
20+
}
21+
return l;
22+
};
23+
const ans: number[] = [];
24+
for (const t of queries) {
25+
let res = 0;
26+
for (let j = 0; j < s.length; ++j) {
27+
const k = search(s, t - s[j], j + 1);
28+
res += n - k;
29+
}
30+
for (const [k, v] of g) {
31+
const a = Math.floor(k / n);
32+
const b = k % n;
33+
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
34+
--res;
35+
}
36+
}
37+
ans.push(res);
38+
}
39+
return ans;
40+
}

0 commit comments

Comments
 (0)