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

 public boolean checkPossibility(int[] nums) {
int cnt = ; //the number of changes
for(int i = ; i < nums.length && cnt<= ; i++){
if(nums[i-] > nums[i]){
cnt++;
if(i-< || nums[i-] <= nums[i])nums[i-] = nums[i]; //modify nums[i-1] of a priority
else nums[i] = nums[i-]; //have to modify nums[i]
}
}
return cnt<=;
}

Non-decreasing Array的更多相关文章

  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. Non-decreasing Array LT665

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

  9. Codeforces831A Unimodal Array

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

  10. Monotonic Array LT896

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

随机推荐

  1. 如何在sqlite3连接中创建并调用自定义函数

    #!/user/bin/env python # @Time :2018/6/8 14:44 # @Author :PGIDYSQ #@File :CreateFunTest.py '''如何在sql ...

  2. Python_if

    if if c语言中的if语句格式如下: if (条件) { 结果} python的格式与其不同,定义了自己的格式,更加的简明: if 条件 : 结果 print(111) if 3 > 2: ...

  3. Python3 视频教程,全网最全的视频教程,爬虫,从入门到实战

    需要联系我:QQ:1844912514 最新Python基础班+就业班视频教程 链接: python分布式爬虫打造搜索引擎链接: https://pan.baidu.com/s/1N7HL7U0gQX ...

  4. 使用Crawler框架搭建自己的爬虫框架MyCrawler

    自己写一个爬虫框架的目的: 完美架构 在实际的数据采集编码过程中,发现代码比较乱,抓取数据,存储数据的代码混杂在一起,为了构建比较完美的数据采集框架 敏捷开发 将数据采集进行标准流程化,每个标准流程都 ...

  5. 使用TCP取样器测试Socket接口

    1 JMeter下载安装 下载地址:JMeter,选择Binaries下面的zip包. 检查java环境,是否安装了jdk或者jre. 解压zip包->找到bin目录下jmeter.bat文件- ...

  6. ES6中6种声明变量的方法

    相关阅读:http://es6.ruanyifeng.com/#docs/let 相关阅读:https://www.cnblogs.com/ksl666/p/5944718.html 相关阅读:htt ...

  7. js的一些点

    1 闭包 闭包就是说,能够读取其他函数内部变量的函数. 其实这句话我不是很明白,因为我觉得闭包的作用是: 延迟函数执行 模拟私有变量 根据第二点的描述,应该是阻止其他东西访问自身私有成员,到了这怎么变 ...

  8. python第十天

    函数 1.函数:完成特定功能的代码块,作为一个整体,对其进行特定的命名,该名字就代表函数 在现实中,很多问题要通过一些工具进行处理==>可以将工具提前生产出来,并命名==>通过名字就可以找 ...

  9. java的集合:List、Set和Map

    虚线是接口,实线是实现类: 集合能够解决的问题:集合可以丽杰为是一种更高级的数组,可以保存多条数据 本质:java官方开发人员基于java的一些基础内容(数组等等)创建了一些接口和类,然后使用这些接口 ...

  10. 多输入select

    目录 多输入select IO模型 select介绍 小demo 注意 引入电子书 title: 多输入select date: 2019/3/20 17:21:34 toc: true --- 多输 ...