From 6a3f0058c08cf9ba526b845fde97d988aa9f4b48 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 10 Jun 2024 11:46:10 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1051.=20Height=20Checker?= =?UTF-8?q?=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/height-checker.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 go/height-checker.go diff --git a/go/height-checker.go b/go/height-checker.go new file mode 100644 index 0000000..8558c1c --- /dev/null +++ b/go/height-checker.go @@ -0,0 +1,25 @@ +package height_checker + +func heightChecker(heights []int) int { + // count the heights + var counts [100]int + for _, h := range heights { + counts[h-1]++ + } + + incorrect := 0 + + expectedH := 1 + for _, h := range heights { + for expectedH <= 100 && counts[expectedH-1] == 0 { + expectedH++ + } + counts[expectedH-1]-- + + if expectedH != h { + incorrect++ + } + } + + return incorrect +}