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 to 1 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].

非递减数列

给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列。

我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n),满足 array[i] <= array[i + 1]

示例 1:

输入: [4,2,3]
输出: True
解释: 你可以通过把第一个4变成1来使得它成为一个非递减数列。

示例 2:

输入: [4,2,1]
输出: False
解释: 你不能在只改变一个元素的情况下将其变为非递减数列。

说明:  n 的范围为 [1, 10,000]。

--------------------------------------------------- ---------------------------------------------------

关键就是当两元素nums[i - 1], nums[i]递减时

nums[i - 2], nums[ i- 1], nums[i]三元素之间的大小关系和变化关系

如果nums[i] > nums[i - 2],那么让nums[i - 1] = nums[i] 即可 如[4, 6, 5] 变为[4, 5, 5]

如果nums[i] < nums[i - 2],那么让nums[i] = nums[i - 1]即可, 如[4, 5, 3] 变为[4, 5, 5]

更改多于一次直接返回False

class Solution:
def checkPossibility(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
modified = False for i in range(len(nums)):
if i > 0:
if nums[i] < nums[i - 1]:
if modified:
return False
if i > 1:
if nums[i] < nums[i - 2]:
nums[i] = nums[i - 1]
else:
nums[i - 1] = nums[i] else:
nums[i - 1] = nums[i]
modified = True
return True

665. Non-decreasing Array的更多相关文章

  1. drawer principle in Combinatorics

    Problem 1: Given an array of real number with length (n2 + 1) A: a1,  a2, ... , an2+1. Prove that th ...

  2. 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 ...

  3. Codeforces 1291 Round #616 (Div. 2) B

    B. Array Sharpening time limit per test1 second memory limit per test256 megabytes inputstandard inp ...

  4. 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 ...

  5. LeetCode 665. 非递减数列(Non-decreasing Array)

    665. 非递减数列 665. Non-decreasing Array 题目描述 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是 ...

  6. 【Leetcode_easy】665. Non-decreasing Array

    problem 665. Non-decreasing Array 题意:是否能够将数组转换为非减数组. solution: 难点在于理解如何对需要修改的元素进行赋值: class Solution ...

  7. 【LeetCode】665. 非递减数列 Non-decreasing Array(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:数组,array,非递减,遍历,python,C++ 目录 题目描述 题目大意 解题方法 一.错误代码 二.举例分析 ...

  8. 665. Non-decreasing Array - LeetCode

    Question 665. Non-decreasing Array Solution 题目大意: 思路:当前判断2的时候可以将当前元素2变为4,也可以将上一个元素4变为2,再判断两变化后是否满足要求 ...

  9. LeetCode 665. Non-decreasing Array (不递减数组)

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

  10. Leetcode 665. Non-decreasing Array(Easy)

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

随机推荐

  1. 8.Appium的基本使用-2(安装node.js)

    node.js 下载地址:https://nodejs.org/en/download/下载 64-bit 下载包下载完成双击安装:

  2. php用户名密码

    http://112.124.47.59:8090/activity/index/free?mobile=15652701923&tcode=f9380859085200714&s=7 ...

  3. kvm虚拟机相关

    一.虚拟机与宿主机鼠标不同步问题: https://blog.csdn.net/u012255731/article/details/53006195 先关闭虚拟机,想要修改鼠标和宿主机界面同步方法如 ...

  4. 使用Quartz框架定时发送预警邮件

    1.  Quartz定时发送预警邮件 1.1.   需求及实现思路 定时查询库存预警信息,一旦存在库存预警的商品,则发邮件通知相关人员 1.2.   Quartz框架 Quartz是OpenSymph ...

  5. spring boot 整合redis --sea 方式1

    application.properties # REDIS (RedisProperties) # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服 ...

  6. leetcode1010

    class Solution: def numPairsDivisibleBy60(self, time: 'List[int]') -> int: sums = 0 s = {} n = le ...

  7. 机器学习入门-数值特征-进行二值化变化 1.Binarizer(进行数据的二值化操作)

    函数说明: 1. Binarizer(threshold=0.9) 将数据进行二值化,threshold表示大于0.9的数据为1,小于0.9的数据为0 对于一些数值型的特征:存在0还有其他的一些数 二 ...

  8. HBASE小结--待续使用

    构建在HDFS之上的分布式,面向列的存储系统,使用zookeeper做协同服务,在需要实时读写和随机访问超大规模数据集的时候使用 缺点:非关系型,不支持SQL,数据类型单一(字符串,无类型),之支持单 ...

  9. 【Maven】项目打包-war包-Jar包[IDEA将项目打成war包]

    [Maven]项目打包-war包-Jar包[IDEA将项目打成war包] 2017年01月31日 00:21:06 阅读数:22912 标签: ideamaven发布博客插件 更多 个人分类: ❷ J ...

  10. winform下利用webBrowser执行javascript

    目前很多网站为了防止恶意提交表单信息,大多都采用了加密的方式对提交信息进行处理,加密处理后通过POST提交给服务器验证,这种操作一般都是用Javascipt进行加密,若是我们想要正确提交表单到网站,就 ...