From 1a95a0e633c4e2d8abcc04a1d018baf107fc9e4f Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 21 Jun 2024 23:04:40 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1052.=20Grumpy=20Bookstore=20?= =?UTF-8?q?Owner=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/grumpy-bookstore-owner.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 go/grumpy-bookstore-owner.go diff --git a/go/grumpy-bookstore-owner.go b/go/grumpy-bookstore-owner.go new file mode 100644 index 0000000..b4044f9 --- /dev/null +++ b/go/grumpy-bookstore-owner.go @@ -0,0 +1,29 @@ +package main + +func maxSatisfied(customers []int, grumpy []int, minutes int) int { + // count always satisfied customers + satisfied := 0 + for i, count := range customers { + satisfied += count * (1 - grumpy[i]) + } + + // get the initial run + unsatisfied := 0 + for i := 0; i < minutes; i++ { + unsatisfied += customers[i] * grumpy[i] + } + + maxCoverage := unsatisfied + for i := minutes; i < len(customers); i++ { + // remove from the start + unsatisfied -= customers[i-minutes] * grumpy[i-minutes] + + // add from the end + unsatisfied += customers[i] * grumpy[i] + + // update max coverage + maxCoverage = max(maxCoverage, unsatisfied) + } + + return maxCoverage + satisfied +}