Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.1782 #1488

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions solution/1700-1799/1782.Count Pairs Of Nodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@

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

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

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

<!-- tabs:start -->

Expand All @@ -78,10 +78,9 @@ class Solution:
g = defaultdict(int)
for a, b in edges:
a, b = a - 1, b - 1
a, b = min(a, b), max(a, b)
cnt[a] += 1
cnt[b] += 1
if a > b:
a, b = b, a
g[(a, b)] += 1

s = sorted(cnt)
Expand Down Expand Up @@ -110,7 +109,7 @@ class Solution {
++cnt[a];
++cnt[b];
int k = Math.min(a, b) * n + Math.max(a, b);
g.put(k, g.getOrDefault(k, 0) + 1);
g.merge(k, 1, Integer::sum);
}
int[] s = cnt.clone();
Arrays.sort(s);
Expand Down Expand Up @@ -220,6 +219,51 @@ func countPairs(n int, edges [][]int, queries []int) []int {
}
```

### **TypeScript**

```ts
function countPairs(n: number, edges: number[][], queries: number[]): number[] {
const cnt: number[] = new Array(n).fill(0);
const g: Map<number, number> = new Map();
for (const [a, b] of edges) {
++cnt[a - 1];
++cnt[b - 1];
const k = Math.min(a - 1, b - 1) * n + Math.max(a - 1, b - 1);
g.set(k, (g.get(k) || 0) + 1);
}
const s = cnt.slice().sort((a, b) => a - b);
const search = (nums: number[], x: number, l: number): number => {
let r = nums.length;
while (l < r) {
const mid = (l + r) >> 1;
if (nums[mid] > x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
const ans: number[] = [];
for (const t of queries) {
let res = 0;
for (let j = 0; j < s.length; ++j) {
const k = search(s, t - s[j], j + 1);
res += n - k;
}
for (const [k, v] of g) {
const a = Math.floor(k / n);
const b = k % n;
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
--res;
}
}
ans.push(res);
}
return ans;
}
```

### **...**

```
Expand Down
50 changes: 47 additions & 3 deletions solution/1700-1799/1782.Count Pairs Of Nodes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ class Solution:
g = defaultdict(int)
for a, b in edges:
a, b = a - 1, b - 1
a, b = min(a, b), max(a, b)
cnt[a] += 1
cnt[b] += 1
if a > b:
a, b = b, a
g[(a, b)] += 1

s = sorted(cnt)
Expand All @@ -95,7 +94,7 @@ class Solution {
++cnt[a];
++cnt[b];
int k = Math.min(a, b) * n + Math.max(a, b);
g.put(k, g.getOrDefault(k, 0) + 1);
g.merge(k, 1, Integer::sum);
}
int[] s = cnt.clone();
Arrays.sort(s);
Expand Down Expand Up @@ -205,6 +204,51 @@ func countPairs(n int, edges [][]int, queries []int) []int {
}
```

### **TypeScript**

```ts
function countPairs(n: number, edges: number[][], queries: number[]): number[] {
const cnt: number[] = new Array(n).fill(0);
const g: Map<number, number> = new Map();
for (const [a, b] of edges) {
++cnt[a - 1];
++cnt[b - 1];
const k = Math.min(a - 1, b - 1) * n + Math.max(a - 1, b - 1);
g.set(k, (g.get(k) || 0) + 1);
}
const s = cnt.slice().sort((a, b) => a - b);
const search = (nums: number[], x: number, l: number): number => {
let r = nums.length;
while (l < r) {
const mid = (l + r) >> 1;
if (nums[mid] > x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
const ans: number[] = [];
for (const t of queries) {
let res = 0;
for (let j = 0; j < s.length; ++j) {
const k = search(s, t - s[j], j + 1);
res += n - k;
}
for (const [k, v] of g) {
const a = Math.floor(k / n);
const b = k % n;
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
--res;
}
}
ans.push(res);
}
return ans;
}
```

### **...**

```
Expand Down
88 changes: 44 additions & 44 deletions solution/1700-1799/1782.Count Pairs Of Nodes/Solution.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
class Solution {
public int[] countPairs(int n, int[][] edges, int[] queries) {
int[] cnt = new int[n];
Map<Integer, Integer> g = new HashMap<>();
for (var e : edges) {
int a = e[0] - 1, b = e[1] - 1;
++cnt[a];
++cnt[b];
int k = Math.min(a, b) * n + Math.max(a, b);
g.put(k, g.getOrDefault(k, 0) + 1);
}
int[] s = cnt.clone();
Arrays.sort(s);
int[] ans = new int[queries.length];
for (int i = 0; i < queries.length; ++i) {
int t = queries[i];
for (int j = 0; j < n; ++j) {
int x = s[j];
int k = search(s, t - x, j + 1);
ans[i] += n - k;
}
for (var e : g.entrySet()) {
int a = e.getKey() / n, b = e.getKey() % n;
int v = e.getValue();
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
--ans[i];
}
}
}
return ans;
}

private int search(int[] arr, int x, int i) {
int left = i, right = arr.length;
while (left < right) {
int mid = (left + right) >> 1;
if (arr[mid] > x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
class Solution {
public int[] countPairs(int n, int[][] edges, int[] queries) {
int[] cnt = new int[n];
Map<Integer, Integer> g = new HashMap<>();
for (var e : edges) {
int a = e[0] - 1, b = e[1] - 1;
++cnt[a];
++cnt[b];
int k = Math.min(a, b) * n + Math.max(a, b);
g.merge(k, 1, Integer::sum);
}
int[] s = cnt.clone();
Arrays.sort(s);
int[] ans = new int[queries.length];
for (int i = 0; i < queries.length; ++i) {
int t = queries[i];
for (int j = 0; j < n; ++j) {
int x = s[j];
int k = search(s, t - x, j + 1);
ans[i] += n - k;
}
for (var e : g.entrySet()) {
int a = e.getKey() / n, b = e.getKey() % n;
int v = e.getValue();
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
--ans[i];
}
}
}
return ans;
}
private int search(int[] arr, int x, int i) {
int left = i, right = arr.length;
while (left < right) {
int mid = (left + right) >> 1;
if (arr[mid] > x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
47 changes: 23 additions & 24 deletions solution/1700-1799/1782.Count Pairs Of Nodes/Solution.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
class Solution:
def countPairs(
self, n: int, edges: List[List[int]], queries: List[int]
) -> List[int]:
cnt = [0] * n
g = defaultdict(int)
for a, b in edges:
a, b = a - 1, b - 1
cnt[a] += 1
cnt[b] += 1
if a > b:
a, b = b, a
g[(a, b)] += 1

s = sorted(cnt)
ans = [0] * len(queries)
for i, t in enumerate(queries):
for j, x in enumerate(s):
k = bisect_right(s, t - x, lo=j + 1)
ans[i] += n - k
for (a, b), v in g.items():
if cnt[a] + cnt[b] > t and cnt[a] + cnt[b] - v <= t:
ans[i] -= 1
return ans
class Solution:
def countPairs(
self, n: int, edges: List[List[int]], queries: List[int]
) -> List[int]:
cnt = [0] * n
g = defaultdict(int)
for a, b in edges:
a, b = a - 1, b - 1
a, b = min(a, b), max(a, b)
cnt[a] += 1
cnt[b] += 1
g[(a, b)] += 1

s = sorted(cnt)
ans = [0] * len(queries)
for i, t in enumerate(queries):
for j, x in enumerate(s):
k = bisect_right(s, t - x, lo=j + 1)
ans[i] += n - k
for (a, b), v in g.items():
if cnt[a] + cnt[b] > t and cnt[a] + cnt[b] - v <= t:
ans[i] -= 1
return ans
40 changes: 40 additions & 0 deletions solution/1700-1799/1782.Count Pairs Of Nodes/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function countPairs(n: number, edges: number[][], queries: number[]): number[] {
const cnt: number[] = new Array(n).fill(0);
const g: Map<number, number> = new Map();
for (const [a, b] of edges) {
++cnt[a - 1];
++cnt[b - 1];
const k = Math.min(a - 1, b - 1) * n + Math.max(a - 1, b - 1);
g.set(k, (g.get(k) || 0) + 1);
}
const s = cnt.slice().sort((a, b) => a - b);
const search = (nums: number[], x: number, l: number): number => {
let r = nums.length;
while (l < r) {
const mid = (l + r) >> 1;
if (nums[mid] > x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
const ans: number[] = [];
for (const t of queries) {
let res = 0;
for (let j = 0; j < s.length; ++j) {
const k = search(s, t - s[j], j + 1);
res += n - k;
}
for (const [k, v] of g) {
const a = Math.floor(k / n);
const b = k % n;
if (cnt[a] + cnt[b] > t && cnt[a] + cnt[b] - v <= t) {
--res;
}
}
ans.push(res);
}
return ans;
}