zoukankan      html  css  js  c++  java
  • 665. Non-decreasing Array只允许修改一位数的非递减数组

    [抄题]:

    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.

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    不知道怎么改啊

    [一句话思路]:

    既然只允许修改一位,“前天”是否异常,决定了应该修改“昨天”还是“今天”

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. 必须要有“昨天”异常的前提才有后续的操作。所以要把“昨天”之后的全都括起来

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    头回见:既然只允许修改一位,“前天”是否异常,决定了应该修改“昨天”还是“今天”

    [复杂度]:Time complexity: O(n) Space complexity: O(1)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    class Solution {
        public boolean checkPossibility(int[] nums) {
            //cc
            if (nums == null || nums.length == 0) {
                return false;
            }
            
            //ini
            int count = 0;
            
            //for loop
            for (int i = 1; i < nums.length && count <= 1; i++) {
                if (nums[i - 1] > nums[i]) {count++;
                if (i - 2 < 0 || nums[i - 2] < nums[i]) {
                    nums[i - 1] = nums[i];
                }else {
                    nums[i] = nums[i - 1];
                }}
            }
            
            //return count <= 1
            return count <= 1;
        }
    }
    View Code
  • 相关阅读:
    MYSQL读写分离
    AIR loadbytes executable code error 解决办法
    文本处理常用命令
    使用awk找出两个大文件的相同部分
    nginx修改php.ini无效的解决办法
    python 正则表达式反向引用的问题
    解决memcached错误:Catastrophic: event fd doesn't match conn fd
    php编译iconv错误解决办法
    grep剔除两个文件中相同部分
    周末闲话
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8901802.html
Copyright © 2011-2022 走看看