Top-$K$ Ranking from Pairwise Comparisons: When Spectral Ranking is Optimal