aboutsummaryrefslogtreecommitdiff
path: root/non_decreasing_array.cpp
blob: 6ea4280f349ae769bd66fdc73dd372b7301936d7 (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
#include "leetcode.h"

class Solution {
public:
  bool checkPossibility(vector<int> &nums) {
    for (int i = 0; i < nums.size() - 1; i++) {
      if (nums[i] > nums[i + 1]) {
        if (i - 1 >= 0 && nums[i - 1] > nums[i + 1])
          nums[i + 1] = nums[i];
        else
          nums[i] = nums[i + 1];
        break;
      }
    }
    for (int i = 0; i < nums.size() - 1; i++) {
      if (nums[i] > nums[i + 1])
        return false;
    }
    return true;
  }
};

int main() {
  Solution obj;
  vector<int> nums = {4, 2, 3};
  cout << obj.checkPossibility(nums);
}