#3616
Medium Algorithms Number of student replacements
Array Simulation
86.2% acceptance
Mar 31, 2026
5
3
You are given an integer array ranks where ranks[i] represents the rank of the ith student arriving in order. A lower number indicates a better rank.
Initially, the first student is selected by default.
A replacement occurs when a student with a strictly better rank arrives and replaces the current selection.
Return the total number of replacements made.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn total_replacements(ranks: Vec<i32>) -> i32 {
let mut count = 0;
let mut best = ranks[0];
for i in 1..ranks.len() {
if ranks[i] < best {
best = ranks[i];
count += 1;
}
}
count
}
}