Go•height-checker/solution.go
package main
import "sort"
func heightChecker(heights []int) int {
expected := append([]int(nil), heights...)
sort.Ints(expected)
mismatches := 0
for index, height := range heights {
if height != expected[index] {
mismatches++
}
}
return mismatches
}
Python•height-checker/solution.py
class Solution:
def heightChecker(self, heights: list[int]) -> int:
return sum(actual != expected for actual, expected in zip(heights, sorted(heights)))
TypeScript•height-checker/solution.ts
function heightChecker(heights: number[]): number {
const expected = [...heights].sort((left, right) => left - right);
return heights.reduce((count, height, index) => count + Number(height !== expected[index]), class="syntax-number">0);
}