/315. Count of Smaller Numbers After Self

315. Count of Smaller Numbers After Self

Hard
Arrays43.3% acceptance

Given an integer array input_array, return an integer array result_array such that result_array[i] is the number of elements in input_array to the right of index i that are strictly less than input_array[i].

Example 1

Input: [7, 3, 8, 2]

Output: [2, 1, 1, 0]

Explanation: To the right of 7: 3 and 2 are smaller (count=2). To the right of 3: 2 is smaller (count=1). To the right of 8: 2 is smaller (count=1). To the right of 2: none are smaller (count=0).

Example 2

Input: [0]

Output: [0]

Explanation: No elements to the right.

Example 3

Input: [4, 4]

Output: [0, 0]

Explanation: No element to the right is smaller for either index.

Constraints

  • 1 <= len(input_array) <= 105
  • -104 <= input_array[i] <= 104
Python (current runtime)

Case 1

Input: [10, 5, 12, 3]

Expected: [2, 1, 1, 0]

Case 2

Input: [1, 2, 3, 4]

Expected: [0, 0, 0, 0]

Case 3

Input: [9, 8, 7, 6]

Expected: [3, 2, 1, 0]

Case 4

Input: [-5, 0, -2, 3]

Expected: [0, 1, 0, 0]

Case 5

Input: [2, 1, 2, 1]

Expected: [2, 0, 1, 0]