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. nginx面试中最常见的18道题

    1.请解释一下什么是Nginx? Nginx是一个web服务器和反向代理服务器,用于HTTP.HTTPS.SMTP.POP3和IMAP协议. 2.请列举Nginx的一些特性. Nginx服务器的特性包 ...

  2. 《算法》第三章部分程序 part 4

    ▶ 书中第三章部分程序,加上自己补充的代码,包括散列表.线性探查表 ● 散列表 package package01; import edu.princeton.cs.algs4.Queue; impo ...

  3. apache 重点难点

    apache 重点难点 在于难以理解其工作原理,因为它是c 写的:其模块众多:功能强大而复杂. 其配置也是格式不齐, 比如一下子是 key value , 一下子就成了 xml. 转载: http:/ ...

  4. SPARK执行流程

    RDD运行原理 1.创建 RDD 对象 2.DAGScheduler模块介入运算,计算RDD之间的依赖关系.RDD之间的依赖关系就形成了DAG 3.每一个JOB被分为多个Stage,划分Stage的一 ...

  5. 清华大学iCenter区块链公开课 第二节

    1.比特币区块的结构 比特币区块结构: 区块大小 区块头 辕老师简版区块: 2.比特币交易结构 输入(可以有多个):比特币来源的UTXO 输出(可以有多个):手续费.接收比特币的地址 总量.锁定脚本尺 ...

  6. 机器学习入门-数值特征-数据四分位特征 1.quantile(用于求给定分数位的数值) 2.plt.axvline(用于画出竖线) 3.pd.pcut(对特征进行分位数切分,生成新的特征)

    函数说明: 1.  .quantile(cut_list) 对DataFrame类型直接使用,用于求出给定列表中分数的数值,这里用来求出4分位出的数值 2.  plt.axvline()  # 用于画 ...

  7. Spark/Storm/Flink

    https://www.cnblogs.com/yaohaitao/p/5703288.html  Spark Streaming与Storm的应用场景 对于Storm来说:1.建议在那种需要纯实时, ...

  8. 8.抽象类、接口和多态.md

    目录 1.抽象类 1.抽象类 2.接口和抽象类的关系 2.1实现上的区别: 22.类和接口的关系: 2.3.Java为什么只能单继承可以多实现: 2.4.接口和接口的关系: 3.多态 2.接口和抽象类 ...

  9. WINDOWS之CMD命令

    用法 1.切换盘符 2.切换到指定盘符后 在使用命令 cd +路径 一般介绍DOS命令,切换工作目录都是用CD命令,但是我在win7下的DOS中使用CD D:\却一直无法转到D盘. 后来在网上查找,发 ...

  10. js文件引用js文件

    我的问题是: a.jsp   b.js  c.js a.jsp  需要引用 b.js 里面的内容 <head> <script type="text/javascript& ...