Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    1782 - Count Pairs Of Nodes\.

    Hard

    You are given an undirected graph defined by an integer n, the number of nodes, and a 2D integer array edges, the edges in the graph, where <code>edgesi = u<sub>i</sub>, v<sub>i</sub></code> indicates that there is an undirected edge between <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. You are also given an integer array queries.

    Let incident(a, b) be defined as the number of edges that are connected to either node a or b.

    The answer to the <code>j<sup>th</sup></code> query is the number of pairs of nodes (a, b) that satisfy both of the following conditions:

    • a &lt; b

    • incident(a, b) &gt; queries[j]

    Return an array answers such that answers.length == queries.length and answers[j] is the answer of the <code>j<sup>th</sup></code> query.

    Note that there can be multiple edges between the same two nodes.

    Example 1:

    Input: n = 4, edges = \[\[1,2],2,4,1,3,2,3,2,1], queries = 2,3

    Output: 6,5

    Explanation: The calculations for incident(a, b) are shown in the table above. The answers for each of the queries are as follows:

    • answers0 = 6. All the pairs have an incident(a, b) value greater than 2.

    • answers1 = 5. All the pairs except (3, 4) have an incident(a, b) value greater than 3.

    Example 2:

    Input: n = 5, edges = \[\[1,5],1,5,3,4,2,5,1,3,5,1,2,3,2,5], queries = 1,2,3,4,5

    Output: 10,10,9,8,6

    Constraints:

    • <code>2 <= n <= 2 * 10<sup>4</sup></code>

    • <code>1 <= edges.length <= 10<sup>5</sup></code>

    • <code>1 <= u<sub>i</sub>, v<sub>i</sub><= n</code>

    • <code>u<sub>i</sub> != v<sub>i</sub></code>

    • 1 &lt;= queries.length &lt;= 20

    • 0 &lt;= queries[j] &lt; edges.length

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final IntArray countPairs(Integer n, Array<IntArray> edges, IntArray queries)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait