diff --git a/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/README.md b/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/README.md index ef26b3b7f4864..5f6bcf5335353 100644 --- a/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/README.md +++ b/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/README.md @@ -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}$ 的长度。 @@ -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; } ``` @@ -153,9 +153,9 @@ function minimumOperations(nums: number[]): number { use std::collections::HashSet; impl Solution { pub fn minimum_operations(nums: Vec) -> i32 { - let mut set = nums.iter().collect::>(); - set.remove(&0); - set.len() as i32 + let mut s = nums.iter().collect::>(); + s.remove(&0); + s.len() as i32 } } ``` diff --git a/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/README_EN.md b/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/README_EN.md index c838453885ea3..2fe4810adaeae 100644 --- a/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/README_EN.md +++ b/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/README_EN.md @@ -66,7 +66,11 @@ In the third operation, choose x = 2. Now, nums = [0,0,0,0,0]. -### 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}$. @@ -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; } ``` @@ -148,9 +152,9 @@ function minimumOperations(nums: number[]): number { use std::collections::HashSet; impl Solution { pub fn minimum_operations(nums: Vec) -> i32 { - let mut set = nums.iter().collect::>(); - set.remove(&0); - set.len() as i32 + let mut s = nums.iter().collect::>(); + s.remove(&0); + s.len() as i32 } } ``` diff --git a/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.rs b/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.rs index f151e5c72c7e4..4ff22f3cef398 100644 --- a/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.rs +++ b/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.rs @@ -1,8 +1,8 @@ use std::collections::HashSet; impl Solution { pub fn minimum_operations(nums: Vec) -> i32 { - let mut set = nums.iter().collect::>(); - set.remove(&0); - set.len() as i32 + let mut s = nums.iter().collect::>(); + s.remove(&0); + s.len() as i32 } } diff --git a/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.ts b/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.ts index 3af64fe7ad831..cf008ba706dba 100644 --- a/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.ts +++ b/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.ts @@ -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; } diff --git a/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/README.md b/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/README.md index a1c8bfc0ec628..ad1b415e313c9 100644 --- a/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/README.md +++ b/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/README.md @@ -160,6 +160,26 @@ function maximumGroups(grades: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_groups(grades: Vec) -> 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 + } +} +``` + diff --git a/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/README_EN.md b/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/README_EN.md index c8e777fea4956..c62526d71ac88 100644 --- a/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/README_EN.md +++ b/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/README_EN.md @@ -65,7 +65,17 @@ It can be shown that it is not possible to form more than 3 groups. -### 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. @@ -150,6 +160,26 @@ function maximumGroups(grades: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_groups(grades: Vec) -> 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 + } +} +``` + diff --git a/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/Solution.rs b/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/Solution.rs new file mode 100644 index 0000000000000..958bb57d2c90b --- /dev/null +++ b/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/Solution.rs @@ -0,0 +1,15 @@ +impl Solution { + pub fn maximum_groups(grades: Vec) -> 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 + } +}