From a58d78d2eb094da7d2ac708a2183d2747dc0303b Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 11 Dec 2024 23:31:31 +0100 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB2779.=20Maximum=20Beauty=20of?= =?UTF-8?q?=20an=20Array=20After=20Applying=20Operation=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/ Signed-off-by: Matej Focko --- ...ty-of-an-array-after-applying-operation.go | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 go/maximum-beauty-of-an-array-after-applying-operation.go diff --git a/go/maximum-beauty-of-an-array-after-applying-operation.go b/go/maximum-beauty-of-an-array-after-applying-operation.go new file mode 100644 index 0000000..d9fde18 --- /dev/null +++ b/go/maximum-beauty-of-an-array-after-applying-operation.go @@ -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 +}