[抄题]:

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;
}
}

665. Non-decreasing Array只允许修改一位数的非递减数组的更多相关文章

  1. Leetcode665.Non-decreasing Array非递减数组

    给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n ...

  2. [LeetCode] Non-decreasing Array 非递减数列

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

  3. 【LeetCode】665. 非递减数列 Non-decreasing Array(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:数组,array,非递减,遍历,python,C++ 目录 题目描述 题目大意 解题方法 一.错误代码 二.举例分析 ...

  4. LeetCode 665. 非递减数列(Non-decreasing Array)

    665. 非递减数列 665. Non-decreasing Array 题目描述 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是 ...

  5. 在当前Server上找某某object,注意只需修改"要找的object"就可以使用

    ---在当前Server上找某某object,注意只需修改"要找的object"就可以使用EXEC sp_MSforeachdb 'use ? ;IF EXISTS(SELECT ...

  6. LeetCode 665. Non-decreasing Array (不递减数组)

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

  7. centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节课

    centos  lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress  安装phpmyadmin  定时备份mysql两种方法  第二十五节 ...

  8. [Swift]LeetCode665. 非递减数列 | Non-decreasing Array

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

  9. Leetcode 665.非递减数列

    非递减数列 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i ...

随机推荐

  1. 从云主机上下载文件到本地+获取外网地址(linux & Windows)

    云主机上下载有集中方法,腾讯论坛有一遍文章:向云服务器上传下载文件方法汇总 货比三家,最终还是选择了rsync 下载代码如下 rsync ubuntu@123.207.251.217:/var/www ...

  2. HDU - 6241 :Color a Tree(不错的二分)

    Bob intends to color the nodes of a tree with a pen. The tree consists of NN nodes. These nodes are ...

  3. [BZOJ5290][HNOI2018]道路

    bzoj luogu sol 考场上普及\(dp\)都做不来,果然是思想僵化了. 设\(f[u][i][j]\)表示在\(u\)点,上方有\(i\)条未修复的公路和\(j\)条未修复的铁路的最小花费. ...

  4. git身份验证失败清除密码缓存

    git config --system --unset credential.helper

  5. 设置ubantu的软件源地址

    查看所用的源 $ sudo vim /etc/apt/sources.list 由于安装的Ubuntu Server 16.04.1 LTS是英文版的,软件源就默认都是 us.archive.ubun ...

  6. Java之父职场路

    Java之父——詹姆斯·高斯林出生于加拿大,是一位计算机编程天才.在卡内基·梅隆大学攻读计算机博士学位时,他编写了多处理器版本的Unix操作系统,是JAVA编程语言的创始人.1991年,在Sun公司工 ...

  7. tensorflow dropout

    我们都知道dropout对于防止过拟合效果不错dropout一般用在全连接的部分,卷积部分不会用到dropout,输出曾也不会使用dropout,适用范围[输入,输出)1.tf.nn.dropout( ...

  8. (转)Android 50道面试题总结(大家看看吧)

    1. 方法重载与覆盖的区别?(Overload与Override的区别)2. String 和StringBuffer的区别3. 字符串“abcde”通过写一个函数不让调用第三方的字符串,实现一个字符 ...

  9. Delphi2010中DataSnap高级技术(转)

    一. 为DataSnap系统服务程序添加描述 这几天一直在研究Delphi 2010的DataSnap,感觉功能真是很强大,现在足有理由证明Delphi7该下岗了. DataSnap有三种服务模式,其 ...

  10. mysql的账号管理

    mysql的账号管理 最先匹配 user 表(包含:用户列 权限列 安全列 资源控制列)连接判断:host  user password字段(user的授权是全局的): 然后匹配db表:如果只是给指定 ...