Skip to content

Commit 5b7d95d

Browse files
committed
Add solution and test-cases for problem 3346
1 parent f03226b commit 5b7d95d

File tree

3 files changed

+76
-26
lines changed

3 files changed

+76
-26
lines changed

leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/README.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [3346.Maximum Frequency of an Element After Performing Operations I][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 two integers `k` and `numOperations`.
5+
6+
You must perform an **operation** `numOperations` times on `nums`, where in each operation you:
7+
8+
- Select an index `i` that was **not** selected in any previous operations.
9+
- Add an integer in the range `[-k, k]` to `nums[i]`.
10+
11+
Return the **maximum** possible frequency of any element in `nums` after performing the **operations**.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
16+
Input: nums = [1,4,5], k = 1, numOperations = 2
17+
18+
Output: 2
1419
15-
## 题意
16-
> ...
20+
Explanation:
1721
18-
## 题解
22+
We can achieve a maximum frequency of two by:
1923
20-
### 思路1
21-
> ...
22-
Maximum Frequency of an Element After Performing Operations I
23-
```go
24+
Adding 0 to nums[1]. nums becomes [1, 4, 5].
25+
Adding -1 to nums[2]. nums becomes [1, 4, 4].
2426
```
2527

28+
**Example 2:**
29+
30+
```
31+
Input: nums = [5,11,20,20], k = 5, numOperations = 1
32+
33+
Output: 2
34+
35+
Explanation:
36+
37+
We can achieve a maximum frequency of two by:
38+
39+
Adding 0 to nums[1].
40+
```
2641

2742
## 结语
2843

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int, numOperations int) int {
4+
const N = 100000 + 2
5+
6+
freq := make([]int, N)
7+
sweep := make([]int, N)
8+
9+
mm := N
10+
MM := 0
11+
12+
for _, x := range nums {
13+
if x >= N {
14+
panic("Value in nums exceeds the array limit N")
15+
}
16+
freq[x]++
17+
18+
x0 := max(1, x-k)
19+
20+
xN := min(x+k+1, N-1)
21+
22+
sweep[x0]++
23+
sweep[xN]--
24+
25+
mm = min(mm, x0)
26+
MM = max(MM, xN)
27+
}
28+
29+
ans := 0
30+
cnt := 0
31+
32+
for x := mm; x <= MM; x++ {
33+
cnt += sweep[x]
34+
35+
max_addable := min(cnt-freq[x], numOperations)
36+
ans = max(ans, freq[x]+max_addable)
37+
}
38+
39+
return ans
540
}

leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
nums []int
14+
k, numOperations int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 4, 5}, 1, 2, 2},
18+
{"TestCase2", []int{5, 11, 20, 20}, 5, 1, 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.nums, c.k, c.numOperations)
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 %v",
27+
c.expect, got, c.nums, c.k, c.numOperations)
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)