zoukankan      html  css  js  c++  java
  • [LeetCode] 1909. Remove One Element to Make the Array Strictly Increasing

    Given a 0-indexed integer array nums, return true if it can be made strictly increasing after removing exactly one element, or false otherwise. If the array is already strictly increasing, return true.

    The array nums is strictly increasing if nums[i - 1] < nums[i] for each index (1 <= i < nums.length).

    Example 1:

    Input: nums = [1,2,10,5,7]
    Output: true
    Explanation: By removing 10 at index 2 from nums, it becomes [1,2,5,7].
    [1,2,5,7] is strictly increasing, so return true.
    

    Example 2:

    Input: nums = [2,3,1,2]
    Output: false
    Explanation:
    [3,1,2] is the result of removing the element at index 0.
    [2,1,2] is the result of removing the element at index 1.
    [2,3,2] is the result of removing the element at index 2.
    [2,3,1] is the result of removing the element at index 3.
    No resulting array is strictly increasing, so return false.

    Example 3:

    Input: nums = [1,1,1]
    Output: false
    Explanation: The result of removing any element is [1,1].
    [1,1] is not strictly increasing, so return false.
    

    Example 4:

    Input: nums = [1,2,3]
    Output: true
    Explanation: [1,2,3] is already strictly increasing, so return true.

    Constraints:

    • 2 <= nums.length <= 1000
    • 1 <= nums[i] <= 1000

    删除一个元素使数组严格递增。

    给你一个下标从 0 开始的整数数组 nums ,如果 恰好 删除 一个 元素后,数组 严格递增 ,那么请你返回 true ,否则返回 false 。如果数组本身已经是严格递增的,请你也返回 true 。

    数组 nums 是 严格递增 的定义为:对于任意下标的 1 <= i < nums.length 都满足 nums[i - 1] < nums[i] 。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/remove-one-element-to-make-the-array-strictly-increasing
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这道题官方给的 tag 是简单题但是一点都不简单。题意是给一个数组,如果最多删除数组中的一个数字就能使得数组整体是单调递增的话,返回 true,否则返回 false。这道题我一开始做的时候切入点就不对,我想的一直是比如找一个中间的元素 nums[i],然后去判断他与 nums[i - 1] 和 nums[i + 1] 的关系。这样做总是有一些case解决不了。

    我参考了discussion的这个帖子,这个帖子把这个题会遇到的case提炼得非常好,

    遍历整个数组,直到找到一个递减的数对,此时大的数为k,小的数为k+1:

    如果k - 1 < 0,说明大的数在开头,删除即可。
    如果nums[k + 1] > nums[k - 1],说明下标为k这个大数是驼峰,删除即可保证递增。
    如果K+ 2 >= n,说明小的数在末尾,删除即可。
    如果nums[k] < nums[k + 2],说明下标为k+1这个小数是低谷,删除即可保证递增。

    作者:seiei
    链接:https://leetcode-cn.com/problems/remove-one-element-to-make-the-array-strictly-increasing/solution/bian-li-yi-bian-shu-zu-zhao-tuo-feng-huo-hvyd/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public boolean canBeIncreasing(int[] nums) {
     3         boolean asc = true;
     4         int len = nums.length;
     5         for (int i = 0; i < n - 1; i++) {
     6             // 如果开始出现降序
     7             if (nums[i] >= nums[i + 1]) {
     8                 if (asc) {
     9                     // 如果是头两个数字乱序或者i是驼峰,把i删除即可
    10                     // nums[i] >= nums[i + 1] && nums[i + 1] >= nums[i - 1]
    11                     if (i - 1 < 0 || nums[i + 1] > nums[i - 1]) {
    12                         asc = false;
    13                     }
    14                     // 如果是最后两个数字乱序或者i是低谷,把i删除即可
    15                     else if (i + 2 >= len || nums[i + 2] > nums[i]) {
    16                         asc = false;
    17                     }
    18                 }
    19                 // 这是第二次降序,没救了,直接返回false
    20                 else {
    21                     return false;
    22                 }
    23             }
    24         }
    25         return true;
    26     }
    27 }

    LeetCode 题目总结

  • 相关阅读:
    进阶算子
    Scala中sortBy和Spark中sortBy区别
    简单算子演示
    map和FlatMap之间区别?
    RDD
    Spark高可用
    Django Rest Framework
    Scrapy
    asyncio
    BeautifulSoup
  • 原文地址:https://www.cnblogs.com/cnoodle/p/15318482.html
Copyright © 2011-2022 走看看