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 a249ab4

Browse files
author
Shuo
authored
Merge pull request #237 from openset/develop
Add: Non-decreasing Array
2 parents 0cb66f4 + 1f01db5 commit a249ab4

File tree

2 files changed

+53
-0
lines changed

2 files changed

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

0 commit comments

Comments
(0)

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