Skip to content

Commit 88a7935

Browse files
committed
feat: add solutions to lc problem: No.2860
No.2860.Happy Students
1 parent 118f64a commit 88a7935

File tree

6 files changed

+171
-4
lines changed

6 files changed

+171
-4
lines changed

solution/2800-2899/2860.Happy Students/README.md

+69-2
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,40 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:排序 + 枚举**
60+
61+
假设选出了 $k$ 个学生,那么以下情况成立:
62+
63+
- 如果 $nums[i] = k$,那么不存在分组方法;
64+
- 如果 $nums[i] \gt k$,那么学生 $i$ 不被选中;
65+
- 如果 $nums[i] \lt k$,那么学生 $i$ 被选中。
66+
67+
因此,被选中的学生一定是排序后的 $nums$ 数组中的前 $k$ 个元素。
68+
69+
我们在 $[0,..n]$ 范围内枚举 $k$,对于当前选出的学生人数 $i$,我们可以得到组内最大的学生编号 $i-1$,数字为 $nums[i-1]$。如果 $i \gt 0$ 并且 $nums[i-1] \ge i$,那么不存在分组方法;如果 $i \lt n$ 并且 $nums[i] \le i$,那么不存在分组方法。否则,存在分组方法,答案加一。
70+
71+
枚举结束后,返回答案即可。
72+
73+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组长度。
74+
5975
<!-- tabs:start -->
6076

6177
### **Python3**
6278

6379
<!-- 这里可写当前语言的特殊实现逻辑 -->
6480

6581
```python
66-
82+
class Solution:
83+
def countWays(self, nums: List[int]) -> int:
84+
nums.sort()
85+
n = len(nums)
86+
ans = 0
87+
for i in range(n + 1):
88+
if i and nums[i - 1] >= i:
89+
continue
90+
if i < n and nums[i] <= i:
91+
continue
92+
return ans
6793
```
6894

6995
### **Java**
@@ -89,13 +115,54 @@ class Solution {
89115
### **C++**
90116

91117
```cpp
92-
118+
class Solution {
119+
public:
120+
int countWays(vector<int>& nums) {
121+
sort(nums.begin(), nums.end());
122+
int ans = 0;
123+
int n = nums.size();
124+
for (int i = 0; i <= n; ++i) {
125+
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
126+
continue;
127+
}
128+
++ans;
129+
}
130+
return ans;
131+
}
132+
};
93133
```
94134
95135
### **Go**
96136
97137
```go
138+
func countWays(nums []int) (ans int) {
139+
sort.Ints(nums)
140+
n := len(nums)
141+
for i := 0; i <= n; i++ {
142+
if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) {
143+
continue
144+
}
145+
ans++
146+
}
147+
return
148+
}
149+
```
150+
151+
### **TypeScript**
98152

153+
```ts
154+
function countWays(nums: number[]): number {
155+
nums.sort((a, b) => a - b);
156+
let ans = 0;
157+
const n = nums.length;
158+
for (let i = 0; i <= n; ++i) {
159+
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
160+
continue;
161+
}
162+
++ans;
163+
}
164+
return ans;
165+
}
99166
```
100167

101168
### **...**

solution/2800-2899/2860.Happy Students/README_EN.md

+53-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,17 @@ The class teacher selects all the students to form the group.
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def countWays(self, nums: List[int]) -> int:
60+
nums.sort()
61+
n = len(nums)
62+
ans = 0
63+
for i in range(n + 1):
64+
if i and nums[i - 1] >= i:
65+
continue
66+
if i < n and nums[i] <= i:
67+
continue
68+
return ans
5969
```
6070

6171
### **Java**
@@ -79,13 +89,54 @@ class Solution {
7989
### **C++**
8090

8191
```cpp
82-
92+
class Solution {
93+
public:
94+
int countWays(vector<int>& nums) {
95+
sort(nums.begin(), nums.end());
96+
int ans = 0;
97+
int n = nums.size();
98+
for (int i = 0; i <= n; ++i) {
99+
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
100+
continue;
101+
}
102+
++ans;
103+
}
104+
return ans;
105+
}
106+
};
83107
```
84108
85109
### **Go**
86110
87111
```go
112+
func countWays(nums []int) (ans int) {
113+
sort.Ints(nums)
114+
n := len(nums)
115+
for i := 0; i <= n; i++ {
116+
if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) {
117+
continue
118+
}
119+
ans++
120+
}
121+
return
122+
}
123+
```
124+
125+
### **TypeScript**
88126

127+
```ts
128+
function countWays(nums: number[]): number {
129+
nums.sort((a, b) => a - b);
130+
let ans = 0;
131+
const n = nums.length;
132+
for (let i = 0; i <= n; ++i) {
133+
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
134+
continue;
135+
}
136+
++ans;
137+
}
138+
return ans;
139+
}
89140
```
90141

91142
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int countWays(vector<int>& nums) {
4+
sort(nums.begin(), nums.end());
5+
int ans = 0;
6+
int n = nums.size();
7+
for (int i = 0; i <= n; ++i) {
8+
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
9+
continue;
10+
}
11+
++ans;
12+
}
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func countWays(nums []int) (ans int) {
2+
sort.Ints(nums)
3+
n := len(nums)
4+
for i := 0; i <= n; i++ {
5+
if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) {
6+
continue
7+
}
8+
ans++
9+
}
10+
return
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def countWays(self, nums: List[int]) -> int:
3+
nums.sort()
4+
n = len(nums)
5+
ans = 0
6+
for i in range(n + 1):
7+
if i and nums[i - 1] >= i:
8+
continue
9+
if i < n and nums[i] <= i:
10+
continue
11+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function countWays(nums: number[]): number {
2+
nums.sort((a, b) => a - b);
3+
let ans = 0;
4+
const n = nums.length;
5+
for (let i = 0; i <= n; ++i) {
6+
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
7+
continue;
8+
}
9+
++ans;
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)