Skip to content

Commit 46fb585

Browse files
authored
feat: add solutions to lc problem: No.2598 (#4785)
1 parent cbd4edb commit 46fb585

File tree

4 files changed

+132
-7
lines changed

4 files changed

+132
-7
lines changed

solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ nums 的 MEX 是 2 。可以证明 2 是可以取到的最大 MEX 。
7676

7777
### 方法一:计数
7878

79-
我们用哈希表或数组 $cnt$ 统计数组中每个数对 $value$ 取模后的余数的个数。
79+
我们用哈希表 $\textit{cnt}$ 统计数组中每个数对 $\textit{value}$ 取模后的余数的个数。
8080

81-
然后从 $0$ 开始遍历,对于当前遍历到的数 $i$,如果 $cnt[i \bmod value]$ 为 $0$,说明数组中不存在一个数对 $value$ 取模后的余数为 $i$,那么 $i$ 就是数组的 MEX,直接返回即可。否则,将 $cnt[i \bmod value]$ 减 $1$,继续遍历。
81+
然后从 $0$ 开始遍历,对于当前遍历到的数 $i$,如果 $\textit{cnt}[i \bmod \textit{value}]$ 为 $0$,说明数组中不存在一个数对 $\textit{value}$ 取模后的余数为 $i$,那么 $i$ 就是数组的 MEX,直接返回即可。否则,将 $\textit{cnt}[i \bmod \textit{value}]$ 减 $1$,继续遍历。
8282

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

8585
<!-- tabs:start -->
8686

@@ -167,6 +167,51 @@ function findSmallestInteger(nums: number[], value: number): number {
167167
}
168168
```
169169

170+
#### Rust
171+
172+
```rust
173+
impl Solution {
174+
pub fn find_smallest_integer(nums: Vec<i32>, value: i32) -> i32 {
175+
let mut cnt = vec![0; value as usize];
176+
for &x in &nums {
177+
let idx = ((x % value + value) % value) as usize;
178+
cnt[idx] += 1;
179+
}
180+
181+
let mut i = 0;
182+
loop {
183+
let idx = (i % value) as usize;
184+
if cnt[idx] == 0 {
185+
return i;
186+
}
187+
cnt[idx] -= 1;
188+
i += 1;
189+
}
190+
}
191+
}
192+
```
193+
194+
#### JavaScript
195+
196+
```js
197+
/**
198+
* @param {number[]} nums
199+
* @param {number} value
200+
* @return {number}
201+
*/
202+
var findSmallestInteger = function (nums, value) {
203+
const cnt = Array(value).fill(0);
204+
for (const x of nums) {
205+
++cnt[((x % value) + value) % value];
206+
}
207+
for (let i = 0; ; ++i) {
208+
if (cnt[i % value]-- === 0) {
209+
return i;
210+
}
211+
}
212+
};
213+
```
214+
170215
<!-- tabs:end -->
171216

172217
<!-- solution:end -->

solution/2500-2599/2598.Smallest Missing Non-negative Integer After Operations/README_EN.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve.
7474

7575
<!-- solution:start -->
7676

77-
### Solution 1: Count
77+
### Solution 1: Counting
7878

79-
We use a hash table or array $cnt$ to count the number of times each remainder of $value$ is taken modulo in the array.
79+
We use a hash table $\textit{cnt}$ to count the number of remainders when each number in the array is modulo $\textit{value}$.
8080

81-
Then start from $0$ and traverse, for the current number $i$ traversed, if $cnt[i \bmod value]$ is $0$, it means that there is no number in the array that takes $i$ modulo $value$ as the remainder, then $i$ is the MEX of the array, and return directly. Otherwise, reduce $cnt[i \bmod value]$ by $1$ and continue to traverse.
81+
Then we traverse starting from $0$. For the current number $i$ being traversed, if $\textit{cnt}[i \bmod \textit{value}]$ is $0$, it means there is no number in the array whose remainder when modulo $\textit{value}$ is $i$, so $i$ is the MEX of the array, and we can return it directly. Otherwise, we decrement $\textit{cnt}[i \bmod \textit{value}]$ by $1$ and continue traversing.
8282

83-
The time complexity is $O(n)$ and the space complexity is $O(value)$. Where $n$ is the length of the array $nums$.
83+
The time complexity is $O(n)$ and the space complexity is $O(\textit{value})$, where $n$ is the length of array $\textit{nums}$.
8484

8585
<!-- tabs:start -->
8686

@@ -167,6 +167,51 @@ function findSmallestInteger(nums: number[], value: number): number {
167167
}
168168
```
169169

170+
#### Rust
171+
172+
```rust
173+
impl Solution {
174+
pub fn find_smallest_integer(nums: Vec<i32>, value: i32) -> i32 {
175+
let mut cnt = vec![0; value as usize];
176+
for &x in &nums {
177+
let idx = ((x % value + value) % value) as usize;
178+
cnt[idx] += 1;
179+
}
180+
181+
let mut i = 0;
182+
loop {
183+
let idx = (i % value) as usize;
184+
if cnt[idx] == 0 {
185+
return i;
186+
}
187+
cnt[idx] -= 1;
188+
i += 1;
189+
}
190+
}
191+
}
192+
```
193+
194+
#### JavaScript
195+
196+
```js
197+
/**
198+
* @param {number[]} nums
199+
* @param {number} value
200+
* @return {number}
201+
*/
202+
var findSmallestInteger = function (nums, value) {
203+
const cnt = Array(value).fill(0);
204+
for (const x of nums) {
205+
++cnt[((x % value) + value) % value];
206+
}
207+
for (let i = 0; ; ++i) {
208+
if (cnt[i % value]-- === 0) {
209+
return i;
210+
}
211+
}
212+
};
213+
```
214+
170215
<!-- tabs:end -->
171216

172217
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} value
4+
* @return {number}
5+
*/
6+
var findSmallestInteger = function (nums, value) {
7+
const cnt = Array(value).fill(0);
8+
for (const x of nums) {
9+
++cnt[((x % value) + value) % value];
10+
}
11+
for (let i = 0; ; ++i) {
12+
if (cnt[i % value]-- === 0) {
13+
return i;
14+
}
15+
}
16+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn find_smallest_integer(nums: Vec<i32>, value: i32) -> i32 {
3+
let mut cnt = vec![0; value as usize];
4+
for &x in &nums {
5+
let idx = ((x % value + value) % value) as usize;
6+
cnt[idx] += 1;
7+
}
8+
9+
let mut i = 0;
10+
loop {
11+
let idx = (i % value) as usize;
12+
if cnt[idx] == 0 {
13+
return i;
14+
}
15+
cnt[idx] -= 1;
16+
i += 1;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)