Skip to content

Commit 24e30f9

Browse files
author
openset
committed
Add: Height Checker
1 parent 6ea17ab commit 24e30f9

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package height_checker
2+
3+
import "sort"
4+
5+
func heightChecker(heights []int) int {
6+
ans, dst := 0, make([]int, len(heights))
7+
copy(dst, heights)
8+
sort.Ints(dst)
9+
for i, v := range dst {
10+
if heights[i] != v {
11+
ans++
12+
}
13+
}
14+
return ans
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package height_checker
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input []int
7+
expected int
8+
}
9+
10+
func TestHeightChecker(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: []int{1, 1, 4, 2, 1, 3},
14+
expected: 3,
15+
},
16+
}
17+
for _, tc := range tests {
18+
output := heightChecker(tc.input)
19+
if output != tc.expected {
20+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)