Skip to content

feat: add solutions to lc problems: No.2357,2358 #4420

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
May 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ tags:

### 方法一:哈希表或数组

我们观察到,每一次操作,都可以把数组 `nums` 中相同且非零的元素减少到 $0$,因此,我们只需要统计数组 `nums` 中有多少个不同的非零元素,即为最少操作数。统计不同的非零元素,可以使用哈希表或数组来实现。
我们观察到,每一次操作,都可以把数组 $\textit{nums}$ 中相同且非零的元素减少到 $0$,因此,我们只需要统计数组 $\textit{nums}$ 中有多少个不同的非零元素,即为最少操作数。统计不同的非零元素,可以使用哈希表或数组来实现。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组长度
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度

<!-- tabs:start -->

Expand Down Expand Up @@ -141,9 +141,9 @@ func minimumOperations(nums []int) (ans int) {

```ts
function minimumOperations(nums: number[]): number {
const set = new Set(nums);
set.delete(0);
return set.size;
const s = new Set(nums);
s.delete(0);
return s.size;
}
```

Expand All @@ -153,9 +153,9 @@ function minimumOperations(nums: number[]): number {
use std::collections::HashSet;
impl Solution {
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
let mut set = nums.iter().collect::<HashSet<&i32>>();
set.remove(&0);
set.len() as i32
let mut s = nums.iter().collect::<HashSet<&i32>>();
s.remove(&0);
s.len() as i32
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ In the third operation, choose x = 2. Now, nums = [0,0,0,0,0].

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table or Array

We observe that in each operation, all identical nonzero elements in the array $\textit{nums}$ can be reduced to $0$. Therefore, we only need to count the number of distinct nonzero elements in $\textit{nums}$, which is the minimum number of operations required. To count the distinct nonzero elements, we can use a hash table or an array.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -136,9 +140,9 @@ func minimumOperations(nums []int) (ans int) {

```ts
function minimumOperations(nums: number[]): number {
const set = new Set(nums);
set.delete(0);
return set.size;
const s = new Set(nums);
s.delete(0);
return s.size;
}
```

Expand All @@ -148,9 +152,9 @@ function minimumOperations(nums: number[]): number {
use std::collections::HashSet;
impl Solution {
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
let mut set = nums.iter().collect::<HashSet<&i32>>();
set.remove(&0);
set.len() as i32
let mut s = nums.iter().collect::<HashSet<&i32>>();
s.remove(&0);
s.len() as i32
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashSet;
impl Solution {
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
let mut set = nums.iter().collect::<HashSet<&i32>>();
set.remove(&0);
set.len() as i32
let mut s = nums.iter().collect::<HashSet<&i32>>();
s.remove(&0);
s.len() as i32
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function minimumOperations(nums: number[]): number {
const set = new Set(nums);
set.delete(0);
return set.size;
const s = new Set(nums);
s.delete(0);
return s.size;
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ function maximumGroups(grades: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_groups(grades: Vec<i32>) -> i32 {
let n = grades.len() as i64;
let (mut l, mut r) = (0i64, n);
while l < r {
let mid = (l + r + 1) / 2;
if mid * mid + mid > 2 * n {
r = mid - 1;
} else {
l = mid;
}
}
l as i32
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,17 @@ It can be shown that it is not possible to form more than 3 groups.

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy + Binary Search

Observing the conditions in the problem, the number of students in the $i$-th group must be less than that in the $(i+1)$-th group, and the total score of students in the $i$-th group must be less than that in the $(i+1)$-th group. We only need to sort the students by their scores in ascending order, and then assign $1$, $2$, ..., $k$ students to each group in order. If the last group does not have enough students for $k$, we can distribute these students to the previous last group.

Therefore, we need to find the largest $k$ such that $\frac{(1 + k) \times k}{2} \leq n$, where $n$ is the total number of students. We can use binary search to solve this.

We define the left boundary of binary search as $l = 1$ and the right boundary as $r = n$. Each time, the midpoint is $mid = \lfloor \frac{l + r + 1}{2} \rfloor$. If $(1 + mid) \times mid \gt 2 \times n$, it means $mid$ is too large, so we shrink the right boundary to $mid - 1$; otherwise, we increase the left boundary to $mid$.

Finally, we return $l$ as the answer.

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

<!-- tabs:start -->

Expand Down Expand Up @@ -150,6 +160,26 @@ function maximumGroups(grades: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_groups(grades: Vec<i32>) -> i32 {
let n = grades.len() as i64;
let (mut l, mut r) = (0i64, n);
while l < r {
let mid = (l + r + 1) / 2;
if mid * mid + mid > 2 * n {
r = mid - 1;
} else {
l = mid;
}
}
l as i32
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
impl Solution {
pub fn maximum_groups(grades: Vec<i32>) -> i32 {
let n = grades.len() as i64;
let (mut l, mut r) = (0i64, n);
while l < r {
let mid = (l + r + 1) / 2;
if mid * mid + mid > 2 * n {
r = mid - 1;
} else {
l = mid;
}
}
l as i32
}
}