Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
# [3347.Maximum Frequency of an Element After Performing Operations II][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given an integer array `nums` and two integers `k` and `numOperations`.

You must perform an **operation** numOperations times on `nums`, where in each operation you:

- Select an index `i` that was **not** selected in any previous operations.
- Add an integer in the range `[-k, k]` to `nums[i]`.

Return the **maximum** possible frequency of any element in `nums` after performing the **operations**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: nums = [1,4,5], k = 1, numOperations = 2

Output: 2

## 题意
> ...
Explanation:

## 题解
We can achieve a maximum frequency of two by:

### 思路1
> ...
Maximum Frequency of an Element After Performing Operations II
```go
Adding 0 to nums[1], after which nums becomes [1, 4, 5].
Adding -1 to nums[2], after which nums becomes [1, 4, 4].
```

**Example 2:**

```
Input: nums = [5,11,20,20], k = 5, numOperations = 1

Output: 2

Explanation:

We can achieve a maximum frequency of two by:

Adding 0 to nums[1].
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,86 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(nums []int, k int, numOperations int) int {
sort.Ints(nums)
ans := 0
numCount := make(map[int]int)
modes := make(map[int]bool)

addMode := func(value int) {
modes[value] = true
if value-k >= nums[0] {
modes[value-k] = true
}
if value+k <= nums[len(nums)-1] {
modes[value+k] = true
}
}

lastNumIndex := 0
for i := 0; i < len(nums); i++ {
if nums[i] != nums[lastNumIndex] {
numCount[nums[lastNumIndex]] = i - lastNumIndex
if i-lastNumIndex > ans {
ans = i - lastNumIndex
}
addMode(nums[lastNumIndex])
lastNumIndex = i
}
}

numCount[nums[lastNumIndex]] = len(nums) - lastNumIndex
if len(nums)-lastNumIndex > ans {
ans = len(nums) - lastNumIndex
}
addMode(nums[lastNumIndex])

leftBound := func(value int) int {
left, right := 0, len(nums)-1
for left < right {
mid := (left + right) / 2
if nums[mid] < value {
left = mid + 1
} else {
right = mid
}
}
return left
}

rightBound := func(value int) int {
left, right := 0, len(nums)-1
for left < right {
mid := (left + right + 1) / 2
if nums[mid] > value {
right = mid - 1
} else {
left = mid
}
}
return left
}

uniqueModes := make([]int, 0, len(modes))
for mode := range modes {
uniqueModes = append(uniqueModes, mode)
}
sort.Ints(uniqueModes)

for _, mode := range uniqueModes {
l := leftBound(mode - k)
r := rightBound(mode + k)
var tempAns int
if count, exists := numCount[mode]; exists {
tempAns = min(r-l+1, count+numOperations)
} else {
tempAns = min(r-l+1, numOperations)
}
if tempAns > ans {
ans = tempAns
}
}

return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
nums []int
k int
numOperations int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 4, 5}, 1, 2, 2},
{"TestCase2", []int{5, 11, 20, 20}, 5, 1, 2},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.k, c.numOperations)
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 %v",
c.expect, got, c.nums, c.k, c.numOperations)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading