[LeetCode] Number of Student Replacements

3616. Number of Student Replacements

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int totalReplacements(vector<int>& ranks) {
int best = ranks[0], res = 0;
for(int i = 1; i < ranks.size(); i++) {
if(best > ranks[i]) {
best = ranks[i];
res++;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/27/PS/LeetCode/number-of-student-replacements/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.