{"id":9219,"date":"2021-12-24T00:31:24","date_gmt":"2021-12-24T08:31:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9219"},"modified":"2021-12-24T00:38:42","modified_gmt":"2021-12-24T08:38:42","slug":"leetcode-1909-remove-one-element-to-make-the-array-strictly-increasing","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1909-remove-one-element-to-make-the-array-strictly-increasing\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1909. Remove One Element to Make the Array Strictly Increasing"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>, return&nbsp;<code>true<\/code>&nbsp;<em>if it can be made&nbsp;<strong>strictly increasing<\/strong>&nbsp;after removing&nbsp;<strong>exactly one<\/strong>&nbsp;element, or&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise. If the array is already strictly increasing, return&nbsp;<\/em><code>true<\/code>.<\/p>\n\n\n\n<p>The array&nbsp;<code>nums<\/code>&nbsp;is&nbsp;<strong>strictly increasing<\/strong>&nbsp;if&nbsp;<code>nums[i - 1] &lt; nums[i]<\/code>&nbsp;for each index&nbsp;<code>(1 &lt;= i &lt; nums.length).<\/code><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,10,5,7]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> By removing 10 at index 2 from nums, it becomes [1,2,5,7].\n[1,2,5,7] is strictly increasing, so return true.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,3,1,2]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong>\n[3,1,2] is the result of removing the element at index 0.\n[2,1,2] is the result of removing the element at index 1.\n[2,3,2] is the result of removing the element at index 2.\n[2,3,1] is the result of removing the element at index 3.\nNo resulting array is strictly increasing, so return false.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,1,1]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> The result of removing any element is [1,1].\n[1,1] is not strictly increasing, so return false.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\n\n\n\n<p>Enumerate the element to remove and check.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(n) -> O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool canBeIncreasing(vector<int>& nums) {\n    const int n = nums.size();\n    auto check = [&](int k) {\n      vector<int> arr(nums);\n      arr.erase(begin(arr) + k);\n      for (int i = 1; i < n - 1; ++i)\n        if (arr[i] <= arr[i - 1]) return false;\n      return true;\n    };\n    for (int i = 0; i < n; ++i)\n      if (check(i)) return true;\n    return false;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/O(1) Space<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool canBeIncreasing(vector<int>& nums) {\n    const int n = nums.size();\n    auto check = [&](int k) {      \n      for (int i = 1; i < n; ++i) {        \n        int j = (i - 1 == k) ? (i - 2) : (i - 1);\n        if (i != k &#038;&#038; j >= 0 && nums[i] <= nums[j])          \n          return false;\n      }\n      return true;\n    };\n    for (int i = 0; i < n; ++i)\n      if (check(i)) return true;\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums, return&nbsp;true&nbsp;if it can be made&nbsp;strictly increasing&nbsp;after removing&nbsp;exactly one&nbsp;element, or&nbsp;false&nbsp;otherwise. If the array is already strictly increasing, return&nbsp;true. The array&nbsp;nums&nbsp;is&nbsp;strictly increasing&nbsp;if&nbsp;nums[i &#8211; 1]&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,749],"class_list":["post-9219","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-gready","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9219"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9219\/revisions"}],"predecessor-version":[{"id":9222,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9219\/revisions\/9222"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}