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:

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

Example 2:

  1. Input: [4,2,1]
  2. Output: False
  3. 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可以更简洁

  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 == 1 || nums[i-2] <= nums[i]) {
  7. nums[i-1] = nums[i];
  8. }
  9. else {
  10. nums[i] = nums[i-1];
  11. }
  12. ++cnt;
  13. }
  14. }
  15.  
  16. return cnt <= 1;
  17. }
  18. }

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

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. Shell:常用+好用命令

    1.#删除15天前的数据 find /usr/local/chen/backup/ -mtime +15 -exec rm -f {} \; 2.release=`echo $name | cut - ...

  2. GC roots

    1.虚拟机栈(本地变量表)引用的对象 2.方法区静态属性引用的对象 3.方法区常量引用的对象 4.本地方法栈JNI(一般指naive方法)中引用的对象   常说的GC(Garbage Collecto ...

  3. 关于python的一些想法

    我来自信息管理与信息系统专业,大一学过c语言但不太精通.学习python是为了学会这门新语言,据了解python会慢慢成为主流编程语言. 因为对绘图方面很感兴趣,希望老师能够在课上多讲一些这方面的东西 ...

  4. Mac系统如何显示隐藏文件?

    显示全部文件 defaults write com.apple.finder AppleShowAllFiles -bool true osascript -e 'tell application & ...

  5. SQL中IF和CASE语句

    IF表达式 IF(A,B,C): 如果 A 是TRUE (A <> 0 and A<> NULL),则 IF()的返回值为B; 否则返回值则为 C.IF() 的返回值为数字值或 ...

  6. 将ipad作为电脑拓展屏或分屏的简单方法

    用Ipad实现电脑分屏的方法是挺简单的,但鉴于部分小白找不到合适的门路,在此重新分享一下. 需要的装备:  ipad   电脑   数据连接线 方法:某宝上搜索 duet display ,只需1元左 ...

  7. inline-block的理解

    首先我们大概区分下 inline与inline-block.block的区别, 官方定义如下: inline:内联元素,从左到右依次排列,宽度高度由内容决定: block:块级元素,独占一行,可以设定 ...

  8. webpack 4.0配置

    webpack一般是本地安装,一般安装webpack webpack-cli,一般是开发依赖上线的时候不需要打包通常npm install webpack webpack-cli  -D安装 安装的时 ...

  9. 深入理解C++11

    [深入理解C++11] 1.很多 现实 的 编译器 都 支持 C99 标准 中的__ func__ 预定 义 标识符 功能, 其 基本 功能 就是 返回 所在 函数 的 名字. 编译器 会 隐式 地 ...

  10. centos7安装 python3.6,且保留2.7版本

    CENTOS7安装PYTHON3.6 1. 安装python3.6可能使用的依赖# yum install openssl-devel bzip2-devel expat-devel gdbm-dev ...