Relative Score Graph Components
Problem statement
Each string in comparisons has the form A B d and means score(A) = (1+d) × score(B). The comparisons are consistent and may form several disconnected components.
If anchors is nonempty, it contains exactly one name score record per component. Return a one-element array containing the globally highest-scoring response.
If anchors is empty, return the highest-scoring response from every component, sorted lexicographically. Break score ties lexicographically.
Function
highestRelativeScores(comparisons: String[], anchors: String[]) → String[]Examples
Example 1
comparisons = ["A B 0.1","B E 0.2","C D -0.5","D E 1.0","F G 0.05"]anchors = ["E 1.0","F 1.05"]return = ["D"]In E's component, D has score 2.0, which is above every other anchored response.
Example 2
comparisons = ["A B 0.1","B D 0.2","E F -0.5"]anchors = []return = ["A","F"]A is highest in the first component and F in the second; names are sorted for the return value.
Example 3
comparisons = ["A B 0.0"]anchors = []return = ["A"]A and B tie, so the lexicographically smaller name wins.
Constraints
1 <= comparisons.length <= 200000.- Names are nonempty ASCII alphanumeric strings of length at most
40. -0.99 < d <= 1000, so every multiplicative factor is positive.- The graph is internally consistent.
anchorsis empty or contains exactly one positive finite score per component.