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. MySQL Connector/C++ 8.0 源码编译

    平台 ubuntu 16.04 参考文档: https://dev.mysql.com/doc/dev/connector-cpp/8.0/building.html 下载源码 访问 https:// ...

  2. Python----数据预处理

    导入标准库 import numpy as np import matplotlib.pyplot as plt import pandas as pd 导入数据集 dataset = pd.read ...

  3. 原型设计的工具-----Axure RP

     原型设计的工具-----Axure RP 1.原型设计的工具 目前能用于原型设计的工具有很多,其中有七种比较好. (1)    Axure RP (2)    Mockplus (3)    Jus ...

  4. 第一章 Python基本语法元素分析(二)

    1.3   实例1:温度转换 根据华氏和摄氏温度定义,利用转换公式如下: C=(F-32)/1.8 F=C*1.8+32 代码如下: 运行结果: 1.4   Python程序语法元素分析 注释:不被程 ...

  5. luogu3687-[ZJOI2017] 仙人掌

    Description P3687 [ZJOI2017]仙人掌 - 洛谷 | 计算机科学教育新生态 Solution 我们先考虑只有一棵树如何处理. 仙人掌可以看做若干环的集合. 特别的, 对于一条没 ...

  6. python学习日记(常用模块)

    模块概念 什么是模块 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代 ...

  7. Linux lvs三种模式工作原理

    LVS:Linux Virtual Sevser,Linux虚拟服务器,由章文嵩先生成立的有关负载均衡的开源项目.基于Linux内核.优化后并发量可以达到百万级. NAT模式: NAT(Network ...

  8. java返回json设置自定义的格式

    使用注解@JsonSerialize(using = CustomPriceSerialize.class) 创建自定义的格式化类(可为内部类) /** * 设置默认返回的小数类型(0.01 元) * ...

  9. python中的技巧——杂记

    杂记 zip的用法 对于 a = [1,2,3] b = [3,2,1] 若要同时遍历 for x, y in zip(a, b): pass zip(a, b)=> [(1,2,3),(3,2 ...

  10. Equipment UVA - 1508(子集补集)

    The Korea Defense and Science Institute, shortly KDSI, has been putting constant effort into newequi ...