-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy path0896-MonotonicArray.cs
More file actions
36 lines (32 loc) · 1007 Bytes
/
0896-MonotonicArray.cs
File metadata and controls
36 lines (32 loc) · 1007 Bytes
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
//-----------------------------------------------------------------------------
// Runtime: 160ms
// Memory Usage: 39.8 MB
// Link: https://leetcode.com/submissions/detail/338646772/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0896_MonotonicArray
{
public bool IsMonotonic(int[] A)
{
if (A.Length <= 2) return true;
var i = 0;
while (i < A.Length - 1 && A[i] == A[i + 1])
i++;
if (i == A.Length - 1) return true;
var isIncreasing = A[i] < A[i + 1];
i++;
if (isIncreasing)
{
for (int k = i; k < A.Length - 1; k++)
if (A[k] > A[k + 1]) return false;
}
else
{
for (int k = i; k < A.Length - 1; k++)
if (A[k] < A[k + 1]) return false;
}
return true;
}
}
}