go: add «2779. Maximum Beauty of an Array After Applying Operation»

URL:	https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-12-11 23:31:31 +01:00
parent a4871df28e
commit a58d78d2eb
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,25 @@
package main
import "slices"
func maximumBeauty(nums []int, k int) int {
if len(nums) == 1 {
return 1
}
maxValue := slices.Max(nums)
count := make([]int, maxValue+1)
for _, n := range nums {
count[max(0, n-k)]++
count[min(n+k+1, maxValue)]--
}
maxBeauty, runningSum := 0, 0
for _, x := range count {
runningSum += x
maxBeauty = max(maxBeauty, runningSum)
}
return maxBeauty
}