Given an array of integers ranks representing the ranks of students visiting an office in order, return the number of times the class monitor is replaced.
The first student to visit becomes the initial monitor. The monitor is replaced whenever a visiting student has a strictly better (lower) rank than the current monitor.
Note that the initial assignment of the first student as monitor is not counted as a replacement.
Function Signature
def count_monitor_replacements(ranks: list[int]) -> int:
Constraints
-
1 <= len(ranks) <= 10^5
-
1 <= ranks[i] <= 10^9
Examples
Input: ranks = [5,3,8,2,7,1,4]
Output: 3
The initial monitor is 5. They are replaced by 3, then by 2, then by 1, for a total of 3 replacements. The students ranked 8, 7 and 4 each rank worse than the monitor at the time they visit.
Input: ranks = [10,10,10,10]
Output: 0
No student has a strictly lower rank than the initial monitor (10), so no replacements occur.