Skip to the code
cf.
+50 XP

#196 Ranking Disagreement

hard

A trending page shows the same set of topics yesterday (before) and today (after), each as a list of distinct topic names from first place to last. To measure how much the ranking changed, count the pairs of topics that the two lists put in opposite order.

Write disagreement(before, after) that returns that count. Both lists contain exactly the same names. Aim for O(n log n).

Example 1

Input: before = ["css","js","html"], after = ["js","css","html"]
Output: 1

Only the pair css/js swapped places.

Example 2

Input: before = ["a","b","c","d"], after = ["d","c","b","a"]
Output: 6

A full reversal flips all 6 pairs.

Constraints

  • 0 <= before.length <= 100000
  • after is a reordering of before.
  • Names are distinct strings.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).