Skip to content

Commit 0123a6a

Browse files
authored
Merge pull request #1344 from 0xff-dev/2598
Add solution and test-cases for problem 2598
2 parents 80550e3 + a923392 commit 0123a6a

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# [2598.Smallest Missing Non-negative Integer After Operations][title]
2+
3+
## Description
4+
You are given a **0-indexed** integer array `nums` and an integer `value`.
5+
6+
In one operation, you can add or subtract `value` from any element of `nums`.
7+
8+
- For example, if `nums = [1,2,3]` and `value = 2`, you can choose to subtract `value` from `nums[0]` to make `nums = [-1,2,3]`.
9+
10+
The MEX (minimum excluded) of an array is the smallest missing **non-negative** integer in it.
11+
12+
- For example, the MEX of `[-1,2,3]` is `0` while the MEX of `[1,0,3]` is `2`.
13+
14+
Return the maximum MEX of `nums` after applying the mentioned operation **any number of times**.
15+
16+
**Example 1:**
17+
18+
```
19+
Input: nums = [1,-10,7,13,6,8], value = 5
20+
Output: 4
21+
Explanation: One can achieve this result by applying the following operations:
22+
- Add value to nums[1] twice to make nums = [1,0,7,13,6,8]
23+
- Subtract value from nums[2] once to make nums = [1,0,2,13,6,8]
24+
- Subtract value from nums[3] twice to make nums = [1,0,2,3,6,8]
25+
The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve.
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: nums = [1,-10,7,13,6,8], value = 7
32+
Output: 2
33+
Explanation: One can achieve this result by applying the following operation:
34+
- subtract value from nums[2] once to make nums = [1,-10,0,13,6,8]
35+
The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve.
36+
```
37+
38+
## 结语
39+
40+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
41+
42+
[title]: https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations
43+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, value int) int {
4+
mp := make([]int, value)
5+
for _, x := range nums {
6+
v := ((x % value) + value) % value
7+
mp[v]++
8+
}
9+
mex := 0
10+
for mp[mex%value] > 0 {
11+
mp[mex%value]--
12+
mex++
13+
}
14+
return mex
515
}

leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
value int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, -10, 7, 13, 6, 8}, 5, 4},
18+
{"TestCase2", []int{1, -10, 7, 13, 6, 8}, 7, 2},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.value)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.value)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)