Skip to content

Commit 6a12c87

Browse files
authored
896.Monotonic Array
1 parent 00ed23e commit 6a12c87

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

896.Monotonic Array

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// T.C - O(n)
2+
// S.C - O(1)
3+
4+
5+
bool isMonotonic(vector<int>& nums) {
6+
bool isIncreasing = true, isDecreasing = true;
7+
for (int i = 1; i < nums.size(); i++) {
8+
if (nums[i] < nums[i - 1]) {
9+
isIncreasing = false;
10+
}
11+
if (nums[i] > nums[i - 1]) {
12+
isDecreasing = false;
13+
}
14+
}
15+
16+
// Return true if the array is increasing or decreasing
17+
return isIncreasing || isDecreasing;
18+
19+
}

0 commit comments

Comments
 (0)