diff --git a/leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/README.md b/leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/README.md new file mode 100644 index 000000000..cbde628d8 --- /dev/null +++ b/leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/README.md @@ -0,0 +1,43 @@ +# [2598.Smallest Missing Non-negative Integer After Operations][title] + +## Description +You are given a **0-indexed** integer array `nums` and an integer `value`. + +In one operation, you can add or subtract `value` from any element of `nums`. + +- 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]`. + +The MEX (minimum excluded) of an array is the smallest missing **non-negative** integer in it. + +- For example, the MEX of `[-1,2,3]` is `0` while the MEX of `[1,0,3]` is `2`. + +Return the maximum MEX of `nums` after applying the mentioned operation **any number of times**. + +**Example 1:** + +``` +Input: nums = [1,-10,7,13,6,8], value = 5 +Output: 4 +Explanation: One can achieve this result by applying the following operations: +- Add value to nums[1] twice to make nums = [1,0,7,13,6,8] +- Subtract value from nums[2] once to make nums = [1,0,2,13,6,8] +- Subtract value from nums[3] twice to make nums = [1,0,2,3,6,8] +The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve. +``` + +**Example 2:** + +``` +Input: nums = [1,-10,7,13,6,8], value = 7 +Output: 2 +Explanation: One can achieve this result by applying the following operation: +- subtract value from nums[2] once to make nums = [1,-10,0,13,6,8] +The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/Solution.go b/leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/Solution.go index d115ccf5e..01937e8f0 100755 --- a/leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/Solution.go +++ b/leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/Solution.go @@ -1,5 +1,15 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, value int) int { + mp := make([]int, value) + for _, x := range nums { + v := ((x % value) + value) % value + mp[v]++ + } + mex := 0 + for mp[mex%value] > 0 { + mp[mex%value]-- + mex++ + } + return mex } diff --git a/leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/Solution_test.go b/leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/Solution_test.go index 14ff50eb4..e572841ed 100755 --- a/leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/Solution_test.go +++ b/leetcode/2501-2600/2598.Smallest-Missing-Non-negative-Integer-After-Operations/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + value int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, -10, 7, 13, 6, 8}, 5, 4}, + {"TestCase2", []int{1, -10, 7, 13, 6, 8}, 7, 2}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.value) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.value) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }