Skip to the code
cf.
+10 XP

#188 Insertion Shift Counts

easy

A to-do app keeps its list sorted by insertion sort as items load: item i is lifted out and slides left past every earlier item that is strictly bigger, each step shifting that bigger item one place to the right.

Write shiftCounts(nums) that returns an array where entry i is how many shifts happen while inserting item i. Entry 0 is always 0.

Example 1

Input: nums = [5,2,4,6,1,3]
Output: [0,1,1,0,4,3]

1 slides past 6, 5, 4, 2 (4 shifts); 3 slides past 6, 5, 4 (3 shifts).

Example 2

Input: nums = [1,2,3]
Output: [0,0,0]

Each item is already bigger than everything before it.

Constraints

  • 0 <= nums.length <= 1000
  • -10^6 <= nums[i] <= 10^6
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).