Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit af042da

Browse files
author
Sandy
authored
Merge pull request #190 from openset/develop
Add: Monotonic Array
2 parents 8aa3cc3 + 5a20356 commit af042da

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
package monotonic_array
2+
3+
func isMonotonic(A []int) bool {
4+
increasing, decreasing := true, true
5+
for i, v := range A[1:] {
6+
if v > A[i] {
7+
decreasing = false
8+
} else if v < A[i] {
9+
increasing = false
10+
}
11+
}
12+
return increasing || decreasing
13+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
11
package monotonic_array
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input []int
7+
expected bool
8+
}
9+
10+
func TestIsMonotonic(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: []int{1, 2, 2, 3},
14+
expected: true,
15+
},
16+
{
17+
input: []int{6, 5, 4, 4},
18+
expected: true,
19+
},
20+
{
21+
input: []int{1, 3, 2},
22+
expected: false,
23+
},
24+
{
25+
input: []int{1, 2, 4, 5},
26+
expected: true,
27+
},
28+
{
29+
input: []int{1, 1, 1},
30+
expected: true,
31+
},
32+
{
33+
input: []int{1, 2, 3, 3, 3, 2, 1},
34+
expected: false,
35+
},
36+
}
37+
for _, tc := range tests {
38+
output := isMonotonic(tc.input)
39+
if output != tc.expected {
40+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
41+
}
42+
}
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /