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. 分布式存储ceph——(1)部署ceph

    前言: 很多朋友想学ceph,但是开始ceph部署就让初学者举步为艰,ceph部署时由于国外源的问题(具体大家应该懂得),下载和安装软件便会卡住,停止不前.即使配置搭建了国内源后,执行ceph-dep ...

  2. MySQL在windows上多次安装失败

    Mysql首次安装: 1.官网下载mysql安装包 2.安装选择自定义,custom 3.更换路径,然后按需求选择,选择标准就行 Mysql重复安装需要注意的问题: 1.程序和功能下,需要卸载MySQ ...

  3. Python----支持向量机SVM

    1.1. SVM介绍 SVM(Support Vector Machines)——支持向量机.其含义是通过支持向量运算的分类器.其中“机”的意思是机器,可以理解为分类器. 1.2. 工作原理 在最大化 ...

  4. Springboot的static和templates区别

    static和templates部分参考博客:https://blog.csdn.net/wangb_java/article/details/71775637 热部署参考博客:https://www ...

  5. 使用py,根据日志记录自动生成周报

    日志格式如下,思路是如果检测到文件中的内容为5位或者8位,即12.11或18.12.11,同时存在.即认为当前行为日期数据仅作为方便查看日志使用,生成脚本时过滤此行.每次读取到空白行的时候则认为下一条 ...

  6. $_SERVER['HTTP_REFERER']的使用

    转载:http://www.5idev.com/p-php_server_http_referer.shtml 使用 $_SERVER['HTTP_REFERER'] 将很容易得到链接到当前页面的前一 ...

  7. mysql时间比较

    ' and ZXBZ ='Y' AND SQRQ >= '2017-04-28 00:00:00' AND SQRQ <= '2017-04-28 23:59:59'; ;

  8. cocos 常用组件

    前面的话 本文将详细介绍 cocos 中的常用组件 Sprite [概述] Sprite(精灵)是 2D 游戏中最常见的显示图像的方式,在节点上添加 Sprite 组件,就可以在场景中显示项目资源中的 ...

  9. win10双系统安装卸载ubuntu

    安装 1. 官网下载需要安装的Ubuntu版本 2. 格式化U盘,用UltraISO软件将Ubuntu写入U盘 3. 设置电脑U盘启动,重启电脑安装,注意安装时关闭在线下载,否则会安装很久 4. 安装 ...

  10. Java面试题之高级篇研读

    1.List和Set比较,各自的子类比较 对比一:ArrayList与LinkedList比较 1.ArrayList是实现了基于动态数组的数据结构,因为地址连续,一旦数据存储好了,查询操作效率会比较 ...