Non-decreasing Array LT665

 Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.

We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).

Example 1:

Input: [4,2,3]
Output: True
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.

 

Example 2:

Input: [4,2,1]
Output: False
Explanation: You can't get a non-decreasing array by modify at most one element.

 

Note: The n belongs to [1, 10,000]. 

Idea 1. 慢慢分析不同情况,

false if more than 1 descending pair

if 1 descending pair, is it possible to do 1 midification? nums[i-1] > nums[i], can we modify nums[i-1] or nums[i]? if nums[i-2] <= nums[i], modifiy nums[i-1] = nums[i]; otherwise, modify nums[i] = nums[i-1], but will fail if nums[i-1] > nums[i+1].

Time complexity: O(n)

Space complexity: O(1)

modify the array while looping it

 1 class Solution {
 2     public boolean checkPossibility(int[] nums) {
 3         boolean decreasing = false;
 4         for(int i = 1; i < nums.length; ++i) {
 5             if(nums[i-1] > nums[i]) {
 6                 
 7                 if(decreasing) {
 8                     return false;
 9                 }
10                 if(i == 1 || nums[i-2] <= nums[i]) {
11                   nums[i-1] = nums[i];
12                 }
13                 else {
14                     nums[i] = nums[i-1];
15                 }
16                 decreasing = true;
17             }
18         }
19         
20         return true;
21     }
22 }

用cnt可以更简洁

class Solution {
    public boolean checkPossibility(int[] nums) {
        int cnt = 0;
        for(int i = 1; cnt <=1 && i < nums.length; ++i) {
            if(nums[i-1] > nums[i]) {
                if(i == 1 || nums[i-2] <= nums[i]) {
                  nums[i-1] = nums[i];
                }
                else {
                    nums[i] = nums[i-1];
                }
                ++cnt;
            }
        }
        
        return cnt <= 1;
    }
}

Idea 1.b 不改变数组

 1 class Solution {
 2     public boolean checkPossibility(int[] nums) {
 3         int cnt = 0;
 4         for(int i = 1; cnt <=1 && i < nums.length; ++i) {
 5             if(nums[i-1] > nums[i]) {
 6                 if( (i>= 2 && nums[i-2] > nums[i])
 7                    && (i+1 < nums.length && nums[i-1] > nums[i+1])) {
 8                   return false;
 9                 }
10                
11                 ++cnt;
12             }
13         }
14         
15         return cnt <= 1;
16     }
17 }

比较好理解的

 1 class Solution {
 2     public boolean checkPossibility(int[] nums) {
 3        int pIndex = -1;
 4        for(int i = 1; i < nums.length; ++i) {
 5            if(nums[i-1] > nums[i]) {
 6                if(pIndex != -1) {
 7                    return false;
 8                }
 9                pIndex = i;
10            }
11        }
12         
13        return (pIndex == -1)
14               || (pIndex == 1) || (pIndex == nums.length-1)
15               || (nums[pIndex-2] <= nums[pIndex])
16               || (nums[pIndex-1] <= nums[pIndex+1]);
17     }
18 }

 

posted on 2019-04-14 22:05  一直走在路上  阅读(129)  评论(0)    收藏  举报

导航