Skip to content

feat: add solutions to lc problem: No.3616 #4567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions solution/3600-3699/3616.Number of Student Replacements/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,94 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3616.Nu

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们用一个变量 $\text{cur}$ 来记录当前选中的学生的排名。遍历数组 $\text{ranks}$,如果遇到一个排名更好的学生(即 $\text{ranks}[i] < \text{cur}$),则更新 $\text{cur}$ 并将答案加一。

遍历结束后,返回答案即可。

时间复杂度 $O(n)$,其中 $n$ 是学生的数量。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python

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
```

#### Java

```java

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;
}
}
```

#### C++

```cpp

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;
}
};
```

#### Go

```go
func totalReplacements(ranks []int) (ans int) {
cur := ranks[0]
for _, x := range ranks {
if x < cur {
cur = x
ans++
}
}
return
}
```

#### TypeScript

```ts
function totalReplacements(ranks: number[]): number {
let [ans, cur] = [0, ranks[0]];
for (const x of ranks) {
if (x < cur) {
cur = x;
ans++;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,94 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3616.Nu

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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

After the iteration, we return the answer.

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

<!-- tabs:start -->

#### Python3

```python

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
```

#### Java

```java

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;
}
}
```

#### C++

```cpp

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;
}
};
```

#### Go

```go
func totalReplacements(ranks []int) (ans int) {
cur := ranks[0]
for _, x := range ranks {
if x < cur {
cur = x
ans++
}
}
return
}
```

#### TypeScript

```ts
function totalReplacements(ranks: number[]): number {
let [ans, cur] = [0, ranks[0]];
for (const x of ranks) {
if (x < cur) {
cur = x;
ans++;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
};
10 changes: 10 additions & 0 deletions solution/3600-3699/3616.Number of Student Replacements/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func totalReplacements(ranks []int) (ans int) {
cur := ranks[0]
for _, x := range ranks {
if x < cur {
cur = x
ans++
}
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
10 changes: 10 additions & 0 deletions solution/3600-3699/3616.Number of Student Replacements/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}