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. MySql CURD操作(数据的增删改查)

    1.增 格式  insert into 表名字 (列名) values(...); 两种方式 1.直接insert into 表名字 values(...);  全部插入 2.insert into ...

  2. dataTable转换成Json格式

    #region dataTable转换成Json格式 /// <summary> /// /// </summary> /// <param name="dt& ...

  3. android studio 模拟器无法联网的解决方法

    本人环境 win10 64位+ android studio  自带的模拟器 1.首先把模拟器打开,然后需要把SDK文件夹下的platform-tools添加到系统变量中(此目录一定要不要弄错). 2 ...

  4. SQL Server死锁诊断--同一行数据在不同索引操作下引起的死锁

    死锁概述 对于数据库中出现的死锁,通俗地解释就是:不同Session(会话)持有一部分资源,并且同时相互排他性地申请对方持有的资源,然后双方都得不到自己想要的资源,从而造成的一种僵持的现象.当然,在任 ...

  5. Win7 Win8 Win10取不到机器码的处理办法

    1.更改UAC也就是用户账户的控制权限 2.以管理员身份运行软件

  6. windows下consul利用json文件注册服务

    windows下,以开发模式启动consul命令 consul agent -dev -config-dir=D:\tools\consul 人工注册服务,新建一个json文件 ,放到D:\tools ...

  7. jQuery-UI的使用

    使用效果图: 源码: <!DOCTYPE html><html lang="en"><head> <meta charset=" ...

  8. angular.min.js:118 Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq?

    1,错误如图所示 简单说下错误原因是:没有js没有注册进去. 解决方法: 1.看下index.html有没有引入你的js文件. 2.看下app.js有没有注册js,比如我这次就是这步没做好,合并代码时 ...

  9. Mysql 常用操作记录

    1.查看数据库: mysql> show databases; +--------------------+ | Database | +--------------------+ | info ...

  10. Linux操作系统df相关问题解惑

    1.df 命令无法使用解决办法 问题分析: 1.1 df命令是通过/etc/mtab文件读取已挂载文件系统的信息,因此,df命令无法使用的原因的在于/etc/mtab文件被损坏或者丢失. 查看 mor ...