go: add «1051. Height Checker»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
4c4dae49a4
commit
6a3f0058c0
1 changed files with 25 additions and 0 deletions
25
go/height-checker.go
Normal file
25
go/height-checker.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue