Skip to content

Commit 5c30065

Browse files
authored
feat: add solutions to lc problem: No.3616 (#4567)
No.3616.Number of Student Replacements
1 parent e9531d1 commit 5c30065

File tree

7 files changed

+187
-8
lines changed

7 files changed

+187
-8
lines changed

solution/3600-3699/3616.Number of Student Replacements/README.md

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,94 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3616.Nu
7070

7171
<!-- solution:start -->
7272

73-
### 方法一
73+
### 方法一:模拟
74+
75+
我们用一个变量 $\text{cur}$ 来记录当前选中的学生的排名。遍历数组 $\text{ranks}$,如果遇到一个排名更好的学生(即 $\text{ranks}[i] < \text{cur}$),则更新 $\text{cur}$ 并将答案加一。
76+
77+
遍历结束后,返回答案即可。
78+
79+
时间复杂度 $O(n)$,其中 $n$ 是学生的数量。空间复杂度 $O(1)$。
7480

7581
<!-- tabs:start -->
7682

7783
#### Python3
7884

7985
```python
80-
86+
class Solution:
87+
def totalReplacements(self, ranks: List[int]) -> int:
88+
ans, cur = 0, ranks[0]
89+
for x in ranks:
90+
if x < cur:
91+
cur = x
92+
ans += 1
93+
return ans
8194
```
8295

8396
#### Java
8497

8598
```java
86-
99+
class Solution {
100+
public int totalReplacements(int[] ranks) {
101+
int ans = 0;
102+
int cur = ranks[0];
103+
for (int x : ranks) {
104+
if (x < cur) {
105+
cur = x;
106+
++ans;
107+
}
108+
}
109+
return ans;
110+
}
111+
}
87112
```
88113

89114
#### C++
90115

91116
```cpp
92-
117+
class Solution {
118+
public:
119+
int totalReplacements(vector<int>& ranks) {
120+
int ans = 0;
121+
int cur = ranks[0];
122+
for (int x : ranks) {
123+
if (x < cur) {
124+
cur = x;
125+
++ans;
126+
}
127+
}
128+
return ans;
129+
}
130+
};
93131
```
94132
95133
#### Go
96134
97135
```go
136+
func totalReplacements(ranks []int) (ans int) {
137+
cur := ranks[0]
138+
for _, x := range ranks {
139+
if x < cur {
140+
cur = x
141+
ans++
142+
}
143+
}
144+
return
145+
}
146+
```
98147

148+
#### TypeScript
149+
150+
```ts
151+
function totalReplacements(ranks: number[]): number {
152+
let [ans, cur] = [0, ranks[0]];
153+
for (const x of ranks) {
154+
if (x < cur) {
155+
cur = x;
156+
ans++;
157+
}
158+
}
159+
return ans;
160+
}
99161
```
100162

101163
<!-- tabs:end -->

solution/3600-3699/3616.Number of Student Replacements/README_EN.md

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,94 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3616.Nu
7070

7171
<!-- solution:start -->
7272

73-
### Solution 1
73+
### Solution 1: Simulation
74+
75+
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.
76+
77+
After the iteration, we return the answer.
78+
79+
The time complexity is $O(n)$, where $n$ is the number of students. The space complexity is $O(1)$.
7480

7581
<!-- tabs:start -->
7682

7783
#### Python3
7884

7985
```python
80-
86+
class Solution:
87+
def totalReplacements(self, ranks: List[int]) -> int:
88+
ans, cur = 0, ranks[0]
89+
for x in ranks:
90+
if x < cur:
91+
cur = x
92+
ans += 1
93+
return ans
8194
```
8295

8396
#### Java
8497

8598
```java
86-
99+
class Solution {
100+
public int totalReplacements(int[] ranks) {
101+
int ans = 0;
102+
int cur = ranks[0];
103+
for (int x : ranks) {
104+
if (x < cur) {
105+
cur = x;
106+
++ans;
107+
}
108+
}
109+
return ans;
110+
}
111+
}
87112
```
88113

89114
#### C++
90115

91116
```cpp
92-
117+
class Solution {
118+
public:
119+
int totalReplacements(vector<int>& ranks) {
120+
int ans = 0;
121+
int cur = ranks[0];
122+
for (int x : ranks) {
123+
if (x < cur) {
124+
cur = x;
125+
++ans;
126+
}
127+
}
128+
return ans;
129+
}
130+
};
93131
```
94132
95133
#### Go
96134
97135
```go
136+
func totalReplacements(ranks []int) (ans int) {
137+
cur := ranks[0]
138+
for _, x := range ranks {
139+
if x < cur {
140+
cur = x
141+
ans++
142+
}
143+
}
144+
return
145+
}
146+
```
98147

148+
#### TypeScript
149+
150+
```ts
151+
function totalReplacements(ranks: number[]): number {
152+
let [ans, cur] = [0, ranks[0]];
153+
for (const x of ranks) {
154+
if (x < cur) {
155+
cur = x;
156+
ans++;
157+
}
158+
}
159+
return ans;
160+
}
99161
```
100162

101163
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int totalReplacements(vector<int>& ranks) {
4+
int ans = 0;
5+
int cur = ranks[0];
6+
for (int x : ranks) {
7+
if (x < cur) {
8+
cur = x;
9+
++ans;
10+
}
11+
}
12+
return ans;
13+
}
14+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func totalReplacements(ranks []int) (ans int) {
2+
cur := ranks[0]
3+
for _, x := range ranks {
4+
if x < cur {
5+
cur = x
6+
ans++
7+
}
8+
}
9+
return
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int totalReplacements(int[] ranks) {
3+
int ans = 0;
4+
int cur = ranks[0];
5+
for (int x : ranks) {
6+
if (x < cur) {
7+
cur = x;
8+
++ans;
9+
}
10+
}
11+
return ans;
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def totalReplacements(self, ranks: List[int]) -> int:
3+
ans, cur = 0, ranks[0]
4+
for x in ranks:
5+
if x < cur:
6+
cur = x
7+
ans += 1
8+
return ans
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function totalReplacements(ranks: number[]): number {
2+
let [ans, cur] = [0, ranks[0]];
3+
for (const x of ranks) {
4+
if (x < cur) {
5+
cur = x;
6+
ans++;
7+
}
8+
}
9+
return ans;
10+
}

0 commit comments

Comments
 (0)