From f71ec980e9ccc9491dd342fbca3e9f46a71cfe18 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 23 Aug 2023 08:46:58 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1782 No.1782.Count Pairs Of Nodes --- .../1782.Count Pairs Of Nodes/README.md | 54 ++++++++++-- .../1782.Count Pairs Of Nodes/README_EN.md | 50 ++++++++++- .../1782.Count Pairs Of Nodes/Solution.java | 88 +++++++++---------- .../1782.Count Pairs Of Nodes/Solution.py | 47 +++++----- .../1782.Count Pairs Of Nodes/Solution.ts | 40 +++++++++ 5 files changed, 203 insertions(+), 76 deletions(-) create mode 100644 solution/1700-1799/1782.Count Pairs Of Nodes/Solution.ts diff --git a/solution/1700-1799/1782.Count Pairs Of Nodes/README.md b/solution/1700-1799/1782.Count Pairs Of Nodes/README.md index 3493cf892a150..28f57f8953259 100644 --- a/solution/1700-1799/1782.Count Pairs Of Nodes/README.md +++ b/solution/1700-1799/1782.Count Pairs Of Nodes/README.md @@ -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$ 是查询数。 @@ -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) @@ -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); @@ -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 = 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; +} +``` + ### **...** ``` diff --git a/solution/1700-1799/1782.Count Pairs Of Nodes/README_EN.md b/solution/1700-1799/1782.Count Pairs Of Nodes/README_EN.md index 35970c14c40eb..abca69726ca59 100644 --- a/solution/1700-1799/1782.Count Pairs Of Nodes/README_EN.md +++ b/solution/1700-1799/1782.Count Pairs Of Nodes/README_EN.md @@ -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) @@ -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); @@ -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 = 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; +} +``` + ### **...** ``` diff --git a/solution/1700-1799/1782.Count Pairs Of Nodes/Solution.java b/solution/1700-1799/1782.Count Pairs Of Nodes/Solution.java index dd928ddad984b..0e0144ceb3eac 100644 --- a/solution/1700-1799/1782.Count Pairs Of Nodes/Solution.java +++ b/solution/1700-1799/1782.Count Pairs Of Nodes/Solution.java @@ -1,45 +1,45 @@ -class Solution { - public int[] countPairs(int n, int[][] edges, int[] queries) { - int[] cnt = new int[n]; - Map 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 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; + } } \ No newline at end of file diff --git a/solution/1700-1799/1782.Count Pairs Of Nodes/Solution.py b/solution/1700-1799/1782.Count Pairs Of Nodes/Solution.py index 4eb730c53cc92..9abc335481ecb 100644 --- a/solution/1700-1799/1782.Count Pairs Of Nodes/Solution.py +++ b/solution/1700-1799/1782.Count Pairs Of Nodes/Solution.py @@ -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 diff --git a/solution/1700-1799/1782.Count Pairs Of Nodes/Solution.ts b/solution/1700-1799/1782.Count Pairs Of Nodes/Solution.ts new file mode 100644 index 0000000000000..32b1a56f43fc3 --- /dev/null +++ b/solution/1700-1799/1782.Count Pairs Of Nodes/Solution.ts @@ -0,0 +1,40 @@ +function countPairs(n: number, edges: number[][], queries: number[]): number[] { + const cnt: number[] = new Array(n).fill(0); + const g: Map = 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; +}