All problems
0842HardArrayBinary SearchSorting

Most Readings Settled at Their Own Slot

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3920Maximize Fixed Points After Deletions

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A log holds the readings nums in the order they were taken. An auditor may delete any readings, including none of them. Deleting a reading closes the gap, so the readings that remain keep their relative order and are renumbered from 0.

A remaining reading is settled when its value equals its slot in the shortened log. Return the largest number of settled readings the auditor can leave behind.

Examples

Example 1

Input
nums = [2, 0, 1, 4]
Output
2

Deleting the reading 2 leaves 0, 1, 4 in slots 0, 1 and 2. The readings 0 and 1 then sit at their own slots, so two readings are settled.

Example 2

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

Deleting the first two readings leaves 2, 1, 0 in slots 0, 1 and 2, where none is settled. Deleting the last four leaves 4 alone, which is not settled either. Keeping all five puts the reading 2 in slot 2, which settles one reading.

Example 3

Input
nums = [0, 0, 0, 0]
Output
1

Only slot 0 can hold a reading of 0, so at most one reading is settled however many are deleted.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^5

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def most_settled_slots(nums: list[int]) -> int:
Java
public int mostSettledSlots(int[] nums)
September 7
Apply