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 first
4
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 ...
随机推荐
- Shell:常用+好用命令
1.#删除15天前的数据 find /usr/local/chen/backup/ -mtime +15 -exec rm -f {} \; 2.release=`echo $name | cut - ...
- GC roots
1.虚拟机栈(本地变量表)引用的对象 2.方法区静态属性引用的对象 3.方法区常量引用的对象 4.本地方法栈JNI(一般指naive方法)中引用的对象 常说的GC(Garbage Collecto ...
- 关于python的一些想法
我来自信息管理与信息系统专业,大一学过c语言但不太精通.学习python是为了学会这门新语言,据了解python会慢慢成为主流编程语言. 因为对绘图方面很感兴趣,希望老师能够在课上多讲一些这方面的东西 ...
- Mac系统如何显示隐藏文件?
显示全部文件 defaults write com.apple.finder AppleShowAllFiles -bool true osascript -e 'tell application & ...
- SQL中IF和CASE语句
IF表达式 IF(A,B,C): 如果 A 是TRUE (A <> 0 and A<> NULL),则 IF()的返回值为B; 否则返回值则为 C.IF() 的返回值为数字值或 ...
- 将ipad作为电脑拓展屏或分屏的简单方法
用Ipad实现电脑分屏的方法是挺简单的,但鉴于部分小白找不到合适的门路,在此重新分享一下. 需要的装备: ipad 电脑 数据连接线 方法:某宝上搜索 duet display ,只需1元左 ...
- inline-block的理解
首先我们大概区分下 inline与inline-block.block的区别, 官方定义如下: inline:内联元素,从左到右依次排列,宽度高度由内容决定: block:块级元素,独占一行,可以设定 ...
- webpack 4.0配置
webpack一般是本地安装,一般安装webpack webpack-cli,一般是开发依赖上线的时候不需要打包通常npm install webpack webpack-cli -D安装 安装的时 ...
- 深入理解C++11
[深入理解C++11] 1.很多 现实 的 编译器 都 支持 C99 标准 中的__ func__ 预定 义 标识符 功能, 其 基本 功能 就是 返回 所在 函数 的 名字. 编译器 会 隐式 地 ...
- centos7安装 python3.6,且保留2.7版本
CENTOS7安装PYTHON3.6 1. 安装python3.6可能使用的依赖# yum install openssl-devel bzip2-devel expat-devel gdbm-dev ...