Non-decreasing Array LT665
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].
Idea 1. 慢慢分析不同情况,
false if more than 1 descending pair
if 1 descending pair, is it possible to do 1 midification? nums[i-1] > nums[i], can we modify nums[i-1] or nums[i]? if nums[i-2] <= nums[i], modifiy nums[i-1] = nums[i]; otherwise, modify nums[i] = nums[i-1], but will fail if nums[i-1] > nums[i+1].
Time complexity: O(n)
Space complexity: O(1)
modify the array while looping it
class Solution {
public boolean checkPossibility(int[] nums) {
boolean decreasing = false;
for(int i = 1; i < nums.length; ++i) {
if(nums[i-1] > nums[i]) {
if(decreasing) {
return false;
}
if(i == 1 || nums[i-2] <= nums[i]) {
nums[i-1] = nums[i];
}
else {
nums[i] = nums[i-1];
}
decreasing = true;
}
}
return true;
}
}
用cnt可以更简洁
class Solution {
public boolean checkPossibility(int[] nums) {
int cnt = 0;
for(int i = 1; cnt <=1 && i < nums.length; ++i) {
if(nums[i-1] > nums[i]) {
if(i == 1 || nums[i-2] <= nums[i]) {
nums[i-1] = nums[i];
}
else {
nums[i] = nums[i-1];
}
++cnt;
}
}
return cnt <= 1;
}
}
Idea 1.b 不改变数组
class Solution {
public boolean checkPossibility(int[] nums) {
int cnt = 0;
for(int i = 1; cnt <=1 && i < nums.length; ++i) {
if(nums[i-1] > nums[i]) {
if( (i>= 2 && nums[i-2] > nums[i])
&& (i+1 < nums.length && nums[i-1] > nums[i+1])) {
return false;
}
++cnt;
}
}
return cnt <= 1;
}
}
比较好理解的
class Solution {
public boolean checkPossibility(int[] nums) {
int pIndex = -1;
for(int i = 1; i < nums.length; ++i) {
if(nums[i-1] > nums[i]) {
if(pIndex != -1) {
return false;
}
pIndex = i;
}
}
return (pIndex == -1)
|| (pIndex == 1) || (pIndex == nums.length-1)
|| (nums[pIndex-2] <= nums[pIndex])
|| (nums[pIndex-1] <= nums[pIndex+1]);
}
}
Non-decreasing Array LT665的更多相关文章
- 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 ...
- 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 ...
随机推荐
- Delphi Exif
这久要用到读取JPG 的 Exif 信息,先是在盒子里下了个Demo,但是那个太老了,是2003年的,后来下载了个CCR 1.5.1是可以使用了,但是我个人用的是Delphi 2007,似乎CCR 1 ...
- Photoshop CC安装与破解方法
下载Photoshop CC与破解补丁 破解补丁就一个文件,amtlib.dll 断网安装Photoshop CC,提示登录选择稍后登录即可 安装成功后将破解补丁安装根目录的amtlib.dll替换即 ...
- DatakeyNames和datakey
DataKey 类用于表示数据绑定控件中某个记录的主键.记录的主键可以由数据源中的一个或多个字段组成.尽管 DataKey 类不是集合,但它可以存储多个键字段值.当调用 DataKey 类的某个构造函 ...
- requests和session的区别
简单说 request对象和session对象的最大区别是生命周期. -request request对象的生命周期是针对一个客户端(说确切点就是一个浏览器应用程序)的一次请求,当请求完毕之后,req ...
- U3D学习资料收集
1,风宇冲的博客 2,gkEngine 3,@浅墨_毛星云 4,聊聊引擎底层如何实现BRDF渲染算法
- supervisord的安装使用
由于生产环境使用的的tomcat,项目比较重要,所以要做进程守护,本来打算自己写脚本,但是效果不理想,想了下还是用supervisord了 由于很久不用,所以写下来部署步骤 第一:安装,安装的方法有y ...
- register关键字
register关键字从c++11开始已经弃用了,但是在看SuRF代码(https://www.cnblogs.com/YuNanlong/p/10235793.html) 的时候,还是看到了这个关键 ...
- 《Dare To Dream》第七次作业:团队项目设计完善&编码测试
任务一:团队项目<软件设计方案说明书>Github链接:https://github.com/Sophur/Team-Project 任务二:项目集成开发环境: (1)JSP技术 JSP( ...
- (转)医疗IT运维系统
http://www.ewei.com/ask/87.html 含义解释 itil运维管理系统,为用户提供专业的it运维管理,对网络运行的状态.故障.性能等监控,又从业务的视角为管理人员提供综合分析和 ...
- Linux 添加中文字体库,解决Java 生成中文水印不显示问题
本机 Windows 环境测试以下代码生成中文水印完全没问题,但是发布到Linux下不显示,一开始以为是报错了没打印出来,搜索发现直接提示中文乱码的或者不显示的,才明白原来是字体库原因,于是开始解决这 ...