External sort run lengths

Write codeHard38 minSnowflake style

Your task

A log compactor sorts a file far larger than memory by writing sorted runs to disk first. It holds memory_slots records at a time. It fills memory from the front of records, then repeatedly writes out the smallest record in memory that is not below the last record it wrote in the current run, and every write is followed at once by reading the next unread record into the slot that just freed. When memory holds nothing that qualifies, the run is closed, a new one opens, and every held record becomes eligible again. Records are integers and may repeat. Return the length of each run in the order the runs are written. Return an empty list for no records or for memory_slots below 1.

Examples

spill_run_lengths(records: list[int], memory_slots: int) -> list[int]
Example 1
Inputspill_run_lengths([5, 1, 9, 3, 7, 2, 8], 3)
Output[6, 1]
Example 2
Inputspill_run_lengths([4, 3, 2, 1], 2)
Output[2, 2]
Example 3
Inputspill_run_lengths([1, 2, 3, 4, 5, 6], 2)
Output[6]