一刷 06/2022
Version #1 Greedy
反正我是想不出来
Time O(N)
Space O(1)
Runtime: 1 ms, faster than 97.53% of Java online submissions for Non-decreasing Array.
Memory Usage: 43.6 MB, less than 83.86% of Java online submissions for Non-decreasing Array.
class Solution {
public boolean checkPossibility(int[] nums) {
// i i+1
// 2 3 4 2 6
// 2 2 4 3 6
boolean violated = false;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i + 1] >= nums[i]) {
continue;
}
if (violated) {
return false;
}
violated = true;
if (i == 0 || nums[i + 1] >= nums[i - 1]) {
nums[i] = nums[i + 1];
} else {
nums[i + 1] = nums[i];
}
}
return true;
}
}
No comments:
Post a Comment