Skip to the code
+50 XP

#194 Lower Scores Later in the Feed

hard

A content feed shows posts in order, each with a quality score in scores. For a "you'll like these less" hint, the app needs, for every post, how many posts after it have a strictly lower score.

Write lowerAfter(scores) that returns an array counts where counts[i] is the number of indexes j > i with scores[j] < scores[i]. Aim for O(n log n).

Example 1

Input: scores = [5,2,6,1]
Output: [2,1,1,0]

After 5 come 2 and 1 (2 lower); after 2 comes 1; after 6 comes 1; nothing comes after 1.

Example 2

Input: scores = [-1,-1]
Output: [0,0]

Equal scores don't count.

Constraints

  • 0 <= scores.length <= 4 * 10^5
  • -10^4 <= scores[i] <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

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