From fcbb0e8e78746525e9e984c68d3d2fa343ba51ec Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 13 Jul 2025 21:15:16 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3616 No.3616.Number of Student Replacements --- .../README.md | 70 +++++++++++++++++-- .../README_EN.md | 70 +++++++++++++++++-- .../Solution.cpp | 14 ++++ .../Solution.go | 10 +++ .../Solution.java | 13 ++++ .../Solution.py | 8 +++ .../Solution.ts | 10 +++ 7 files changed, 187 insertions(+), 8 deletions(-) create mode 100644 solution/3600-3699/3616.Number of Student Replacements/Solution.cpp create mode 100644 solution/3600-3699/3616.Number of Student Replacements/Solution.go create mode 100644 solution/3600-3699/3616.Number of Student Replacements/Solution.java create mode 100644 solution/3600-3699/3616.Number of Student Replacements/Solution.py create mode 100644 solution/3600-3699/3616.Number of Student Replacements/Solution.ts diff --git a/solution/3600-3699/3616.Number of Student Replacements/README.md b/solution/3600-3699/3616.Number of Student Replacements/README.md index c73c104131dcf..1bbd5d5ee7a70 100644 --- a/solution/3600-3699/3616.Number of Student Replacements/README.md +++ b/solution/3600-3699/3616.Number of Student Replacements/README.md @@ -70,32 +70,94 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3616.Nu -### 方法一 +### 方法一:模拟 + +我们用一个变量 $\text{cur}$ 来记录当前选中的学生的排名。遍历数组 $\text{ranks}$,如果遇到一个排名更好的学生(即 $\text{ranks}[i] < \text{cur}$),则更新 $\text{cur}$ 并将答案加一。 + +遍历结束后,返回答案即可。 + +时间复杂度 $O(n)$,其中 $n$ 是学生的数量。空间复杂度 $O(1)$。 #### 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& 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; +} ``` diff --git a/solution/3600-3699/3616.Number of Student Replacements/README_EN.md b/solution/3600-3699/3616.Number of Student Replacements/README_EN.md index dce2bfc7ee3f2..57e52f484ff73 100644 --- a/solution/3600-3699/3616.Number of Student Replacements/README_EN.md +++ b/solution/3600-3699/3616.Number of Student Replacements/README_EN.md @@ -70,32 +70,94 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3616.Nu -### 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)$. #### 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& 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; +} ``` diff --git a/solution/3600-3699/3616.Number of Student Replacements/Solution.cpp b/solution/3600-3699/3616.Number of Student Replacements/Solution.cpp new file mode 100644 index 0000000000000..9ccdb1569d991 --- /dev/null +++ b/solution/3600-3699/3616.Number of Student Replacements/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int totalReplacements(vector& ranks) { + int ans = 0; + int cur = ranks[0]; + for (int x : ranks) { + if (x < cur) { + cur = x; + ++ans; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3600-3699/3616.Number of Student Replacements/Solution.go b/solution/3600-3699/3616.Number of Student Replacements/Solution.go new file mode 100644 index 0000000000000..383dd6c9a3679 --- /dev/null +++ b/solution/3600-3699/3616.Number of Student Replacements/Solution.go @@ -0,0 +1,10 @@ +func totalReplacements(ranks []int) (ans int) { + cur := ranks[0] + for _, x := range ranks { + if x < cur { + cur = x + ans++ + } + } + return +} \ No newline at end of file diff --git a/solution/3600-3699/3616.Number of Student Replacements/Solution.java b/solution/3600-3699/3616.Number of Student Replacements/Solution.java new file mode 100644 index 0000000000000..f7023980f84f7 --- /dev/null +++ b/solution/3600-3699/3616.Number of Student Replacements/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/3600-3699/3616.Number of Student Replacements/Solution.py b/solution/3600-3699/3616.Number of Student Replacements/Solution.py new file mode 100644 index 0000000000000..d6948c4598e22 --- /dev/null +++ b/solution/3600-3699/3616.Number of Student Replacements/Solution.py @@ -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 diff --git a/solution/3600-3699/3616.Number of Student Replacements/Solution.ts b/solution/3600-3699/3616.Number of Student Replacements/Solution.ts new file mode 100644 index 0000000000000..bb31cc615436d --- /dev/null +++ b/solution/3600-3699/3616.Number of Student Replacements/Solution.ts @@ -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; +}