n nodes numbered 0 to n - 1. Every node is painted: node i has the colour given by the letter colors[i]. The edges are listed in links, where links[j] = [a, b] is an edge from a to b.
A path is a sequence of nodes where each node has an edge to the next one (a single node is a path too). The value of a path is how many times its most frequent colour appears on it.
Return the largest value over all paths in the graph. If the graph contains a cycle, return -1.colors="rgrbr", links=[[0, 1], [1, 2], [0, 3], [3, 2], [2, 4]] → 3
The path 0 → 1 → 2 → 4 visits three nodes coloured r (0, 2 and 4); no path does better.
colors="xyx", links=[[0, 1], [1, 2], [2, 1]] → -1
Nodes 1 and 2 form a cycle, so the answer is -1.
colors="qq", links=[] → 1
colors consists of lowercase English letterscolors="rgrbr", links=[[0, 1], [1, 2], [0, 3], [3, 2], [2, 4]]
3
Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.