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

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

用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 不改变数组

 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>= 2 && nums[i-2] > nums[i])
&& (i+1 < nums.length && nums[i-1] > nums[i+1])) {
return false;
} ++cnt;
}
} return cnt <= 1;
}
}

比较好理解的

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

Non-decreasing Array LT665的更多相关文章

  1. drawer principle in Combinatorics

    Problem 1: Given an array of real number with length (n2 + 1) A: a1,  a2, ... , an2+1. Prove that th ...

  2. Maximum Width Ramp LT962

    Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j].  The ...

  3. Codeforces 1291 Round #616 (Div. 2) B

    B. Array Sharpening time limit per test1 second memory limit per test256 megabytes inputstandard inp ...

  4. 5403. Find the Kth Smallest Sum of a Matrix With Sorted Rows

    You are given an m * n matrix, mat, and an integer k, which has its rows sorted in non-decreasing or ...

  5. LeetCode Minimum Moves to Equal Array Elements

    原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...

  6. Leetcode: Sort Transformed Array

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  7. [Swift]LeetCode896. 单调数列 | Monotonic Array

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  8. Codeforces831A Unimodal Array

    A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. Monotonic Array LT896

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

随机推荐

  1. Git 记不住命令

    Git 记不住命令 每次用每次查 真棒 git log --stat --author=someone # git查询某个人修改记录 git log filename # fileName相关的com ...

  2. loadrunner 上传下载

    转http://blog.163.com/yings_9371/blog/static/66196922010711115545137/ (1)LoadRunner上传文件 web_submit_da ...

  3. 啊哈算法第四章第二节解救小哈Java实现

    package corejava; public class FourTwo { static int m;//(m,n)为几行几列 static int n; static int p;//(p,q ...

  4. DRF框架之认证组件用法(第四天)

    1. 什么是drf 框架的认证组件: auth 就等于是jango中的Auth模块,Auth是自带session信息,但是 drf的认证组件可以自定义token携带过去,去判断用的 2.如何实现认证呢 ...

  5. 【399】jupyter 修改主题

    参考:Jupyter 主题更换 参考:Restoring default theme #86 修改主题的方法: 首先在 cmd 上输入 jt -l 选择自己需要的主题,如 jt -t monokai ...

  6. MongoDB 集合(Collection)对应的物理文件

    dbpath下是清一色的collection-n-***与index-n-***开头的物理文件,如何知道某一个集合与其对应与其对应的物理文件? db.collection_name.stats() 返 ...

  7. CSS 背景图像 重复图像

    重复图像 background-repeat 属性可以重复图像,这对于小图片来说是福音. background-repeat 属性有6个值: repeat 背景图像在垂直方向和水平方向都重复 repe ...

  8. 《转》完美解决微信video视频隐藏控件和内联播放问题

    地址:https://blog.csdn.net/xiao190128/article/details/81025378 var u = navigator.userAgent; var isAndr ...

  9. CSRF总结

    一.CSRF概述 CSRF跨站请求伪造,2007年被列为互联网20大安全隐患之一. 什么是跨站请求伪造?CSRF或XSRF 挟制用户在当前已经登录的web应用程序上执行非本意的操作的攻击方法.攻击者盗 ...

  10. spring AOP capbilities and goal

    Spring AOP 是用纯JAVA 实现的. 不需借助JAVA代码在编译处理阶段来实现. Spring 是在运行期实现的.AOP的实现可以在编译,加载,运行三个阶段来实现:Spring AOP 也不 ...