aboutsummaryrefslogtreecommitdiff
path: root/store/works/solutions/leetcode/cpp/896.cpp
blob: 25bd5289dd577760f1a69583f5255f94de6c1981 (plain)
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
#include <vector>

using std::vector;

enum Order { Ascend, Descend, Unknown };

class Solution {
public:
  bool isMonotonic(vector<int> &A) {
    Order order = Unknown;

    int count = A.size();
    for (int i = 1; i < count; i++) {
      int last = A[i - 1];
      int current = A[i];
      if (order == Unknown) {
        if (current > last) {
          order = Ascend;
        } else if (current < last) {
          order = Descend;
        }
      } else if (order == Ascend) {
        if (last > current) {
          return false;
        }
      } else {
        if (last < current) {
          return false;
        }
      }
    }

    return true;
  }
};