Skip to content

Commit f74b972

Browse files
authored
Merge pull request #1059 from 0xff-dev/2779
Add solution and test-cases for problem 2779
2 parents 4fe59ad + 634fec4 commit f74b972

File tree

3 files changed

+91
-12
lines changed

3 files changed

+91
-12
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [2779.Maximum Beauty of an Array After Applying Operation][title]
2+
3+
## Description
4+
You are given a **0-indexed** array nums and a **non-negative** integer `k`.
5+
6+
In one operation, you can do the following:
7+
8+
- Choose an index `i` that **hasn't been chosen before** from the range `[0, nums.length - 1]`.
9+
- Replace `nums[i]` with any integer from the range `[nums[i] - k, nums[i] + k]`.
10+
11+
The **beauty** of the array is the length of the longest subsequence consisting of equal elements.
12+
13+
Return the **maximum** possible beauty of the array `nums` after applying the operation any number of times.
14+
15+
**Note** that you can apply the operation to each index **only once**.
16+
17+
A **subsequence** of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.
18+
19+
**Example 1:**
20+
21+
```
22+
Input: nums = [4,6,1,2], k = 2
23+
Output: 3
24+
Explanation: In this example, we apply the following operations:
25+
- Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2].
26+
- Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4].
27+
After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3).
28+
It can be proven that 3 is the maximum possible length we can achieve.
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input: nums = [1,1,1,1], k = 10
35+
Output: 4
36+
Explanation: In this example we don't have to apply any operations.
37+
The beauty of the array nums is 4 (whole array).
38+
```
39+
40+
## 结语
41+
42+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
43+
44+
[title]: https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation
45+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int, k int) int {
6+
sort.Ints(nums)
7+
ans := 0
8+
start, end := 0, 0
9+
// 排序以后,能够出现交集的情况就是
10+
// 最小的元素定义了右边界
11+
// 最大的元素定义了左边界
12+
// re 就是交集的右边界
13+
re := nums[0] + k
14+
15+
for ; end < len(nums); end++ {
16+
// 还在同一个范围
17+
s := nums[end] - k
18+
if s <= re {
19+
// 还有可以增加de
20+
continue
21+
}
22+
diff := end - start
23+
ans = max(ans, diff)
24+
25+
// 开始减少左侧的数据
26+
// 尝试移除start
27+
start++
28+
for ; start <= end; start++ {
29+
re = nums[start] + k
30+
if s <= re {
31+
break
32+
}
33+
}
34+
}
35+
diff := end - start
36+
ans = max(ans, diff)
37+
38+
return ans
539
}

leetcode/2701-2800/2779.Maximum-Beauty-of-an-Array-After-Applying-Operation/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+
nums []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{4, 6, 1, 2}, 2, 3},
18+
{"TestCase2", []int{1, 1, 1, 1}, 10, 4},
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)
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.nums, 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)