Problem · Sorting
Signal Pings
Learn this problemProblem statement
You have an array of n binary signals, where each signal initially has a value of 0. There are n different pings made to these signals, changing their value from 0 to 1. The i ping affects the signal at index ping[i].
After each ping, the processor sorts the array by performing sweeps from left to right, swapping adjacent elements where signal[i] = 1 and signal[i + 1] = 0. The processor stops when no swaps are made in a sweep.
Determine the number of sweeps required after each ping to sort the array.
Note: Each signal is only pinged once.
Endless thanks to the friend who so generously shared the problem source! 🌸
Function
getRequiredSweeps(ping: int[]) → int[]Examples
Example 1
ping = [1, 2, 4, 3]return = [2, 3, 3, 1]