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:
parent
a4871df28e
commit
a58d78d2eb
1 changed files with 25 additions and 0 deletions
25
go/maximum-beauty-of-an-array-after-applying-operation.go
Normal file
25
go/maximum-beauty-of-an-array-after-applying-operation.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue