Non-decreasing Array
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 first4to1to 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的更多相关文章
- drawer principle in Combinatorics
Problem 1: Given an array of real number with length (n2 + 1) A: a1, a2, ... , an2+1. Prove that th ...
- 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 ...
- Codeforces 1291 Round #616 (Div. 2) B
B. Array Sharpening time limit per test1 second memory limit per test256 megabytes inputstandard inp ...
- 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 ...
- LeetCode Minimum Moves to Equal Array Elements
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...
- 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( ...
- [Swift]LeetCode896. 单调数列 | Monotonic Array
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- Non-decreasing Array LT665
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- Codeforces831A Unimodal Array
A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Monotonic Array LT896
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
随机推荐
- 不能完整读取txt文件问题
txt文件内容 5 1.3 0.4 3.4 -1.7 16.7 0.89 14.17 4.8 1.34 0.42 3.36 -2 16.2 0.9 14.8 4.9 1.30 0.37 3.51 -1 ...
- python面试终极准备
简述Python的深浅拷贝? 将列表内的元素,根据位数合并成字典 lst = [1,2,4,8,16,32,64,128,256,512,1024,32769,65536,4294967296] # ...
- Django-6 Django ORM层
ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...
- 能ping通虚拟机中的Ubuntu,使用XShell连不上
1.在宿主机上telnet 虚拟机ip 22如果显示端口无法接通,说明你的/etc/init.d/sshd 是stop或者是异常的. 2.如果没有sshd服务,使用" sudo apt-g ...
- plus调用android原生页面
var main = plus.android.runtimeMainActivity(); var Intent = plus.android.importClass("android.c ...
- Vue.js 2.x笔记:表单绑定(3)
1. 基础用法 v-model 指令:在表单 input 和 textarea 元素上创建双向数据绑定. 1.1 单行文本(Text) <div id="app"> & ...
- Ansible小记
参考网址: https://www.iyunv.com/thread-385359-1-1.html http://blog.51cto.com/215687833/1886305
- git的安装(和远程仓库建立连接)
安装完git 1.配置用户名和邮箱 $ git config --global user.name "My Name" $ git config --global user.em ...
- iOS XIB使用中适配iPhoneX的安全区域、调用UiView动画
2.调用UiView动画 WeakSelf; self.detailsViewBom.constant += 230; [UIView animateWithDuration:animotiontim ...
- java篇 之 流程控制语句
条件判断语句 条件语句: If(boolean类型) {} else {} (打大括号避免出错) switch (export)语句:export的类型必须是 byte,short,char,i ...