Skip to content

Commit 5e267d8

Browse files
committed
Runtime: 1534 ms (Top 45.54%) | Memory: 199 MB (Top 25.74%)
1 parent 5eb781c commit 5e267d8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

scripts/algorithms/C/Count Pairs Of Nodes/Count Pairs Of Nodes.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
// Runtime: 1534 ms (Top 45.54%) | Memory: 199 MB (Top 25.74%)
12
class Solution {
23
public:
34
map<pair<int, int>, int>edgeCount;
45
vector<int>degree;
5-
6+
67
vector<int> countPairsHelper(vector<int>&indegree, vector<int>&queries, int n){
78
vector<int>ans;
8-
9+
910
// for each query, time: O(2*n) for two pointer and O(uniqueEdgesCount)
10-
for(int query: queries){
11-
// two pointer
11+
for(int query: queries){
12+
// two pointer
1213
int i = 0, j = n-1, total = 0;
1314
while(i != j){
1415
if(degree[i] + degree[j] > query){
@@ -23,8 +24,7 @@ class Solution {
2324
total += (n-i-1);
2425
i++;
2526
}
26-
27-
27+
2828
// remove the negative contribution of edgeCount from all the pairs so far counted
2929
for(auto i: edgeCount){
3030
int u = i.first.first, v = i.first.second, w = i.second;
@@ -38,7 +38,7 @@ class Solution {
3838
return ans;
3939
}
4040
vector<int> countPairs(int n, vector<vector<int>>& edges, vector<int>& queries) {
41-
41+
4242
vector<int>indegree(n, 0);
4343
for(auto edge: edges){
4444
int x = edge[0], y = edge[1];
@@ -49,11 +49,11 @@ class Solution {
4949
indegree[u]++;
5050
indegree[v]++;
5151
}
52-
52+
5353
for(int i = 0; i < n; ++i){
5454
degree.push_back(indegree[i]);
5555
}
5656
sort(degree.begin(), degree.end()); // sort degree to apply smart two pointer
5757
return countPairsHelper(indegree, queries, n);
5858
}
59-
};
59+
};

0 commit comments

Comments
 (0)