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. SpringMVC model 多余字段 忽略

    spring-mybaits的model中如何通过注解忽略非数据库字段?——CSDN问答频道https://ask.csdn.net/questions/643534 ObjectMapper忽略多余 ...

  2. js 解决两值交换

    总结七种办法来交换a和b的变量值 交换变量值方案一 最最最简单的办法就是使用一个临时变量了 不过使用临时变量的方法实在是太low了 var t; t = a; a = b; b = t;   首先把a ...

  3. 内存泄漏(Memory Leak)

    什么情况下会导致内存泄露(Memory Leak)? Android 的虚拟机是基于寄存器的Dalvik,它的最大堆大小一般是16M,有的机器为24M.因此我们所能利用 的内存空间是有限的.如果我们的 ...

  4. 【UOJ448】【集训队作业2018】人类的本质 min_25筛

    题目大意 给你 \(n,m\),求 \[ \sum_{i=1}^n\sum_{x_1,x_2,\ldots,x_m=1}^i\operatorname{lcm}(\gcd(i,x_1),\gcd(i, ...

  5. Shell 简单构建 Node web服务器

    .git bash 执行代码生成: ./makeJs.sh 生成文件如下: 访问:http://127.0.0.1:3030/index.html makeJs.sh  代码如下: #create m ...

  6. Mac 上的 GLFW 环境配置

    背景:GLFW 一.下载和编译 从官网下载源代码包:http://www.glfw.org/download.html (我下载的是 github 仓库上的) 按官方指南编译.总结如下: cd glf ...

  7. Mac 使用 OpenMP/Clang

    新建 hello.cpp 文件: #include <omp.h> #include <stdio.h> int main() { #pragma omp parallel p ...

  8. [Windows Hyper-V-Server]Enable or disable firewall rules under powershell / powershell下启用禁用防火墙规则

    http://www.cryer.co.uk/brian/windows/hyper-v-server/help_computer_cannot_be_managed.htm Enable COM+ ...

  9. Miller_Rabbin&&Pollard_Rho 学习笔记

    占坑,待填 I Intro 首先我们考虑这样一个问题 给定一个正整数\(p(p<=1e8)\),请判断它是不是质数 妈妈我会试除法! 于是,我们枚举$ \sqrt p$ 以内的所有数,就可以非常 ...

  10. 记录下扣jio的2018年

    踩着18年的尾巴,写下这篇总结,既给18年画上句号,也展望19年,制定下计划. 自17年底正式接手团队项目管理工作以来,虽然前面一年都干了大部分工作,但正式走到这个位置上来,还是有一部分的期待.接手之 ...