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 first4
to1
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].
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 ...
随机推荐
- 【MySQL】5.7 复制
参考:http://www.cnblogs.com/zhoujinyi/p/5704567.html 参考:http://www.innomysql.com/article/25656.html 参考 ...
- Zabbix 配置监控主机
1.新建主机: zabbix中的主机(Host)是要监控的网络实体(物理的,或者虚拟的);zabbix中,对于主机的定义非常灵活,它可以时一台物理服务器,一个网络交换机,一个虚拟机或者一些应用 zab ...
- windows文件名格式的中文+数字混合字符串排序
记录一下 [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)] private static extern int StrCm ...
- 读取tensorflow的checkpoint里保存的参数
import tensorflow as tf from tensorflow.python import pywrap_tensorflow import os checkpoint_path = ...
- 实现ls-l功能
实现代码 #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <diren ...
- Python 3.7 安装Twisted
win10电脑 64位系统 Python 3.7 版本安装Twisted-18.9.0-cp37-cp37m-win32.whl 会成功 先下载下来,放到 Scripts文件夹下 然后cmd 我的是 ...
- linux 之用户管理
用户的家目录 普通用户:/home/用户名/,所有者和所属组都是此用户,权限是700 超级用户:/root/,所有者和所属组都是root用户,权限是550 用户邮箱目录 /var/spool/mail ...
- jQuery横向上下排列鱼骨图形式信息展示代码时光轴样式(转自CSDN,原文链接附于文中)
原文链接:http://www.jqueryfuns.com/resource/2173 $.fn.fishBone = function(data) { var colors = ['#F89782 ...
- [原创] debian 9.3 搭建Jira+Confluence+Bitbucket项目管理工具(四) -- 安装bitbucket 5.7.0
[原创] debian 9.3 搭建Jira+Confluence+Bitbucket项目管理工具(四) -- 安装bitbucket 5.7.0 安装Bitbucket的教程, 网上能找见的不多, ...
- 其它 搭建https服务器
原因是这样的,做了个淘宝的数据管理系统(仅供自己使用),然后需要淘宝卖家的生意参谋里面的一些数据,比如实时访客,里面有每个用户搜索什么关键字进来的,这些信息. 自己基于CefSharp开发了一个win ...