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 ...
随机推荐
- 爬虫之scrapy
一.项目简单流程 1.创建项目 scrapy startproject 项目名 2.创建Spider cd 项目名 scrapy genspider 爬虫名 域名 class YokaSpider(s ...
- 快乐!ajax入门(1)
今天试着默写ajax时出现了神秘的问题,出现如图所示的错误: 百度了一下,说是跨源问题,我以为放在同一个文件夹不也是同源嘛!结果打扰了,属实是弟弟,协议,域名,端口相同的算同源,其他的不是!!! 最后 ...
- EL表达式与JSTL标签map遍历varStatus属性下标使用
在JSP页面开发中,JSTL标签库迭代标签<c:forEach>为我们迭代遍历数组集合提供了一种选择. 遍历过程中varStatus属性为我们遍历集合提升了很大操作空间. 贴一下具体使用 ...
- [Flutter] 写第一个 Flutter app,part1 要点
模拟器中调试元素的布局: Android Studio 右侧边栏 Flutter Inspector,选择 Toggle Debug Paint 打开. 格式化代码: 编辑器中右键 Reformat ...
- Redis 编译安装
系统学习一下,记录一下笔记,之前都是断断续续尝试过一些简单的安装使用 下载,解压 编译安装 copy配置文件 启动连接 ./bin/redis-server ./redis.conf 登陆./bin/ ...
- centos7下部署mariadb+galera数据库高可用集群
[root@node1 ~]# cat /etc/yum.repos.d/mariadb.repo # MariaDB 10.1 CentOS repository list - created 20 ...
- ARTS打卡计划第一周-Tips-ControllerAdvice的使用
通常在开发具体项目过程中我们可能会面临如下问题: 统一所有的json返回结果 统一处理所有controller中的异常,并且给不同异常不同的返回状态值 统一对返回的接口做数据校验或者加密,防止篡改 在 ...
- EXPRESS项目PM2启动NODE_ENV传参数不生效问题解决方法
expree项目开发完,涉及到不同环境,要在启动到时候就要配置好环境变量, packge.json文件如下: "scripts": { "dev": " ...
- SVM-sklearn
目的:1000张数字0-9的手写数字,训练识别手写数字:将其作为32*32的0,1化的数字,随后会将其变为1024列的一个向量 原理:SVM就是把平面的点变为一个空间的点,更好切,核函数就是怎么把他变 ...
- kafka创建会话,报Error while executing topic command : Replication factor: 1 larger than available brokers: 0.
bin/kafka-topics.sh --create --zookeeper es1:2181 --replication-factor 1 --partitions 1 --topic top ...