-
Notifications
You must be signed in to change notification settings - Fork 1
/
LC896.cpp
49 lines (35 loc) · 1009 Bytes
/
LC896.cpp
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
43
44
45
46
47
48
49
/*
An array is monotonic if it is either monotone increasing or monotone decreasing.
An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].
Given an integer array nums, return true if the given array is monotonic, or false otherwise.
Example 1:
Input: nums = [1,2,2,3]
Output: true
Example 2:
Input: nums = [6,5,4,4]
Output: true
Example 3:
Input: nums = [1,3,2]
Output: false
Example 4:
Input: nums = [1,2,4,5]
Output: true
Example 5:
Input: nums = [1,1,1]
Output: true
*/
class Solution {
public:
bool isMonotonic(vector<int>& A) {
bool isNonDecreasing = true;
bool isNonIncreasing = true;
for (int i = 1; i < A.size(); i++)
{
if (A[i] < A[i - 1])
isNonDecreasing = false;
if (A[i] > A[i - 1])
isNonIncreasing = false;
}
return isNonDecreasing | isNonIncreasing;
}
};