First Eligible Partner in a Social Graph
Problem statement
You are given an undirected social graph with people numbered from 0 through n - 1. Each row [u, v] in edges is one social link. The array eligible has length n, where eligible[i] == 1 means person i satisfies the partner condition.
Starting from start, return the eligible person reachable with the fewest social hops. Do not consider start as their own partner. If several eligible people are equally close, return the smaller person ID. Return -1 when no eligible partner is reachable.
Function
findFirstEligiblePartner(n: int, edges: int[][], start: int, eligible: int[]) → intExamples
Example 1
n = 6edges = [[0,1],[0,2],[1,3],[2,4],[3,5]]start = 0eligible = [0,0,0,1,1,1]return = 3People 3 and 4 are both two hops away. The smaller ID, 3, wins the tie.
Example 2
n = 4edges = [[0,1]]start = 0eligible = [1,0,1,0]return = -1The starting person is excluded, and the only other eligible person is disconnected.
Example 3
n = 5edges = [[0,4],[0,2],[4,3]]start = 0eligible = [0,0,1,0,1]return = 2People 2 and 4 are each one hop away, so the smaller ID is returned.
Constraints
1 <= n <= 100000.0 <= edges.length <= 200000.- Every edge contains two different valid person IDs; edges are unique and represent undirected links.
eligible.length == nand every value is0or1.0 <= start < n.