NewThe Learning module is now liveSee what's new

A head librarian is implementing a new system to organize incoming book shipments onto shelves. There are numShelves shelves numbered from 0 to numShelves - 1, and initially, every shelf is empty (0 books).

The library receives a batch of books to be shelved one by one. For the ithi^{\text{th}} book in the sequence, there is a restriction limit[i]. This book must be placed on the shelf with the minimum number of books currently on it, considering only the shelves from index 0 to limit[i] - 1.

If there are multiple shelves within that range having the same minimum number of books, the librarian chooses the shelf with the smallest index.

Given numShelves and the array limit, find the shelf index assigned to every book in the shipment.

Input: numShelves = 5, limit = [3, 2, 3, 2, 4] Output: [0, 1, 2, 0, 3] Explanation:

Book LimitShelf Counts (Current)Assigned ToRemarks
3[0, 0, 0, 0, 0]0Look at shelves 0 to 2. All have 0 books. Tie-break: choose the lowest index 0.
2[1, 0, 0, 0, 0]1Look at shelves 0 to 1. Shelf 1 has 0 books (min). Shelf 0 has 1. Choose 1.
3[1, 1, 0, 0, 0]2Look at shelves 0 to 2. Shelf 2 has 0 books. Choose 2.
2[1, 1, 1, 0, 0]0Look at shelves 0 to 1. Both have 1 book. Tie-break: choose the lowest index 0.
4[2, 1, 1, 0, 0]3Look at shelves 0 to 3. Shelf 3 has 0 books. Choose 3.

Example 2

Input: numShelves = 3, limit = [2, 1, 3, 2, 2] Output: [0, 0, 1, 1, 0]

Example 3

Input: numShelves = 5, limit = [4, 3, 5, 1, 4, 5] Output: [0, 1, 2, 0, 3, 4]

Constraints:

  • 1 ≤ numShelves ≤ 10510^5
  • 1 ≤ limit.length ≤ 10510^5
  • 1 ≤ limit[i] ≤ numShelves

Accepted
1.1k/2k
Acceptance Rate54.8%
Test Cases
Input
numServers =
requests =
Output
Expected