forked from qiyuangong/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path665_Non-decreasing_Array.java
More file actions
29 lines (28 loc) · 1002 Bytes
/
665_Non-decreasing_Array.java
File metadata and controls
29 lines (28 loc) · 1002 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
class Solution {
/* public boolean checkPossibility(int[] nums) {
int pos = -1;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) {
// More than two broken points
if (pos != -1) return false;
pos = i;
}
}
if (pos == -1 || pos == 0 || pos == nums.length - 2) return true;
// Remove pos or pos + 1
return (nums[pos - 1] <= nums[pos + 1] || nums[pos] <= nums[pos + 2]);
} */
public boolean checkPossibility(int[] nums) {
int brokenPoint = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) {
brokenPoint++;
if (brokenPoint >= 2) return false;
// Remove i or remove i + 1
if (i - 1 < 0 || nums[i - 1] <= nums[i + 1]) nums[i] = nums[i + 1];
else nums[i + 1] = nums[i];
}
}
return true;
}
}