Skip to content

Commit 4bea707

Browse files
committed
Add solution and test-cases for problem 3397
1 parent f03226b commit 4bea707

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

leetcode/3301-3400/3397.Maximum-Number-of-Distinct-Elements-After-Operations/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [3397.Maximum Number of Distinct Elements After Operations][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `nums` and an integer `k`.
5+
6+
You are allowed to perform the following **operation** on each element of the array **at most** once:
7+
8+
- Add an integer in the range `[-k, k]` to the element.
9+
10+
Return the **maximum** possible number of **distinct** elements in `nums` after performing the **operations**.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
15+
Input: nums = [1,2,2,3,3,4], k = 2
1416
15-
## 题意
16-
> ...
17+
Output: 6
1718
18-
## 题解
19+
Explanation:
1920
20-
### 思路1
21-
> ...
22-
Maximum Number of Distinct Elements After Operations
23-
```go
21+
nums changes to [-1, 0, 1, 2, 3, 4] after performing operations on the first four elements.
2422
```
2523

24+
**Example 2:**
25+
26+
```
27+
Input: nums = [4,4,4,4], k = 1
28+
29+
Output: 3
30+
31+
Explanation:
32+
33+
By adding -1 to nums[0] and 1 to nums[1], nums changes to [3, 5, 4, 4].
34+
```
2635

2736
## 结语
2837

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"math"
5+
"sort"
6+
)
7+
8+
func Solution(nums []int, k int) int {
9+
sort.Ints(nums)
10+
cnt := 0
11+
prev := math.MinInt32
12+
13+
for _, num := range nums {
14+
curr := min(max(num-k, prev+1), num+k)
15+
if curr > prev {
16+
cnt++
17+
prev = curr
18+
}
19+
}
20+
return cnt
521
}

leetcode/3301-3400/3397.Maximum-Number-of-Distinct-Elements-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+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2, 2, 3, 3, 4}, 2, 6},
18+
{"TestCase2", []int{4, 4, 4, 4}, 1, 3},
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.k)
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.k)
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)