-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
42 lines (41 loc) · 1.16 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
namespace MonotonicArray
{
class Program
{
static void Main(string[] args)
{
int[] A = {1, 2, 3, 4};
Console.WriteLine(IsMonotonic(A));
}
static bool IsMonotonic(int[] A)
{
if (A.Length == 1)
return true;
const string increase = "increase";
const string decrease = "decrease";
const string start = "start";
string state = start;
for (int i = 1; i < A.Length; i++)
{
if (A[i] == A[i - 1])
continue;
switch (state)
{
case start:
state = A[i] > A[i - 1] ? increase : decrease;
break;
case increase:
if (A[i] < A[i - 1])
return false;
break;
case decrease:
if (A[i] > A[i - 1])
return false;
break;
}
}
return true;
}
}
}