diff --git a/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/README.md b/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/README.md index 7ac632cae..671b4fd1e 100755 --- a/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/README.md +++ b/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/README.md @@ -1,28 +1,43 @@ # [3346.Maximum Frequency of an Element After Performing Operations I][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 I -```go +Adding 0 to nums[1]. nums becomes [1, 4, 5]. +Adding -1 to nums[2]. 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]. +``` ## 结语 diff --git a/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/Solution.go b/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/Solution.go index d115ccf5e..4486ad09f 100644 --- a/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/Solution.go +++ b/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/Solution.go @@ -1,5 +1,40 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, k int, numOperations int) int { + const N = 100000 + 2 + + freq := make([]int, N) + sweep := make([]int, N) + + mm := N + MM := 0 + + for _, x := range nums { + if x >= N { + panic("Value in nums exceeds the array limit N") + } + freq[x]++ + + x0 := max(1, x-k) + + xN := min(x+k+1, N-1) + + sweep[x0]++ + sweep[xN]-- + + mm = min(mm, x0) + MM = max(MM, xN) + } + + ans := 0 + cnt := 0 + + for x := mm; x <= MM; x++ { + cnt += sweep[x] + + max_addable := min(cnt-freq[x], numOperations) + ans = max(ans, freq[x]+max_addable) + } + + return ans } diff --git a/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/Solution_test.go b/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/Solution_test.go index 14ff50eb4..ff42f21c4 100644 --- a/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/Solution_test.go +++ b/leetcode/3301-3400/3346.Maximum-Frequency-of-an-Element-After-Performing-Operations-I/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + nums []int + k, 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() { }