Skip to content

3616. Number of Student Replacements πŸ”’

DifficultyMedium

Description

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.

 

Example 1:

Input: ranks = [4,1,2]

Output: 1

Explanation:

  • The first student with ranks[0] = 4 is initially selected.
  • The second student with ranks[1] = 1 is better than the current selection, so a replacement occurs.
  • The third student has a worse rank, so no replacement occurs.
  • Thus, the number of replacements is 1.

Example 2:

Input: ranks = [2,2,3]

Output: 0

Explanation:

  • The first student with ranks[0] = 2 is initially selected.
  • Neither of ranks[1] = 2 or ranks[2] = 3 is better than the current selection.
  • Thus, the number of replacements is 0.

 

Constraints:

  • 1 <= ranks.length <= 105​​​​​​​
  • 1 <= ranks[i] <= 105

Solutions

Solution 1: Simulation

Thinking

A replacement occurs exactly when a strictly better (smaller) rank appears. Keep the current chosen rank \(\textit{cur}\), initialized to the first student.

Scan left to right; if \(x<\textit{cur}\), update \(\textit{cur}\) and increment the answer. \(n\le 10^5\) allows a single pass; the history of ranks is unnecessary.

We use a variable \(\textit{cur}\) to record the rank of the currently selected student. We iterate through the array \(\textit{ranks}\), and if we encounter a student with a better rank (i.e., \(\textit{ranks}[i] < \textit{cur}\)), we update \(\textit{cur}\) and increment the answer by one.

After the iteration, we return the answer.

The time complexity is \(O(n)\), and the space complexity is \(O(1)\), where \(n\) is the number of students.

1
2
3
4
5
6
7
8
class Solution:
    def totalReplacements(self, ranks: list[int]) -> int:
        ans, cur = 0, ranks[0]
        for x in ranks:
            if x < cur:
                cur = x
                ans += 1
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    public int totalReplacements(int[] ranks) {
        int ans = 0;
        int cur = ranks[0];
        for (int x : ranks) {
            if (x < cur) {
                cur = x;
                ++ans;
            }
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
public:
    int totalReplacements(vector<int>& ranks) {
        int ans = 0;
        int cur = ranks[0];
        for (int x : ranks) {
            if (x < cur) {
                cur = x;
                ++ans;
            }
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
func totalReplacements(ranks []int) (ans int) {
    cur := ranks[0]
    for _, x := range ranks {
        if x < cur {
            cur = x
            ans++
        }
    }
    return
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function totalReplacements(ranks: number[]): number {
    let [ans, cur] = [0, ranks[0]];
    for (const x of ranks) {
        if (x < cur) {
            cur = x;
            ans++;
        }
    }
    return ans;
}

Comments