Skip to content

Commit 684f58d

Browse files
authored
Merge pull request #1349 from 0xff-dev/3347
Add solution and test-cases for problem 3347
2 parents 31f6ac7 + ebb05e1 commit 684f58d

File tree

3 files changed

+123
-26
lines changed

3 files changed

+123
-26
lines changed

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [3347.Maximum Frequency of an Element After Performing Operations II][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 II
23-
```go
24+
Adding 0 to nums[1], after which nums becomes [1, 4, 5].
25+
Adding -1 to nums[2], after which 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: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,86 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int, k int, numOperations int) int {
6+
sort.Ints(nums)
7+
ans := 0
8+
numCount := make(map[int]int)
9+
modes := make(map[int]bool)
10+
11+
addMode := func(value int) {
12+
modes[value] = true
13+
if value-k >= nums[0] {
14+
modes[value-k] = true
15+
}
16+
if value+k <= nums[len(nums)-1] {
17+
modes[value+k] = true
18+
}
19+
}
20+
21+
lastNumIndex := 0
22+
for i := 0; i < len(nums); i++ {
23+
if nums[i] != nums[lastNumIndex] {
24+
numCount[nums[lastNumIndex]] = i - lastNumIndex
25+
if i-lastNumIndex > ans {
26+
ans = i - lastNumIndex
27+
}
28+
addMode(nums[lastNumIndex])
29+
lastNumIndex = i
30+
}
31+
}
32+
33+
numCount[nums[lastNumIndex]] = len(nums) - lastNumIndex
34+
if len(nums)-lastNumIndex > ans {
35+
ans = len(nums) - lastNumIndex
36+
}
37+
addMode(nums[lastNumIndex])
38+
39+
leftBound := func(value int) int {
40+
left, right := 0, len(nums)-1
41+
for left < right {
42+
mid := (left + right) / 2
43+
if nums[mid] < value {
44+
left = mid + 1
45+
} else {
46+
right = mid
47+
}
48+
}
49+
return left
50+
}
51+
52+
rightBound := func(value int) int {
53+
left, right := 0, len(nums)-1
54+
for left < right {
55+
mid := (left + right + 1) / 2
56+
if nums[mid] > value {
57+
right = mid - 1
58+
} else {
59+
left = mid
60+
}
61+
}
62+
return left
63+
}
64+
65+
uniqueModes := make([]int, 0, len(modes))
66+
for mode := range modes {
67+
uniqueModes = append(uniqueModes, mode)
68+
}
69+
sort.Ints(uniqueModes)
70+
71+
for _, mode := range uniqueModes {
72+
l := leftBound(mode - k)
73+
r := rightBound(mode + k)
74+
var tempAns int
75+
if count, exists := numCount[mode]; exists {
76+
tempAns = min(r-l+1, count+numOperations)
77+
} else {
78+
tempAns = min(r-l+1, numOperations)
79+
}
80+
if tempAns > ans {
81+
ans = tempAns
82+
}
83+
}
84+
85+
return ans
586
}

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ 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 int
15+
numOperations int
16+
expect int
1517
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
18+
{"TestCase1", []int{1, 4, 5}, 1, 2, 2},
19+
{"TestCase2", []int{5, 11, 20, 20}, 5, 1, 2},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.nums, c.k, c.numOperations)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.nums, c.k, c.numOperations)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)