返回介绍

solution / 1900-1999 / 1909.Remove One Element to Make the Array Strictly Increasing / README_EN

发布于 2024-06-17 01:03:12 字数 5335 浏览 0 评论 0 收藏 0

1909. Remove One Element to Make the Array Strictly Increasing

中文文档

Description

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.

 

Constraints:

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

Solutions

Solution 1

class Solution:
  def canBeIncreasing(self, nums: List[int]) -> bool:
    def check(nums, i):
      prev = -inf
      for j, num in enumerate(nums):
        if i == j:
          continue
        if prev >= nums[j]:
          return False
        prev = nums[j]
      return True

    i, n = 1, len(nums)
    while i < n and nums[i - 1] < nums[i]:
      i += 1
    return check(nums, i - 1) or check(nums, i)
class Solution {
  public boolean canBeIncreasing(int[] nums) {
    int i = 1, n = nums.length;
    for (; i < n && nums[i - 1] < nums[i]; ++i)
      ;
    return check(nums, i - 1) || check(nums, i);
  }

  private boolean check(int[] nums, int i) {
    int prev = Integer.MIN_VALUE;
    for (int j = 0; j < nums.length; ++j) {
      if (i == j) {
        continue;
      }
      if (prev >= nums[j]) {
        return false;
      }
      prev = nums[j];
    }
    return true;
  }
}
class Solution {
public:
  bool canBeIncreasing(vector<int>& nums) {
    int i = 1, n = nums.size();
    for (; i < n && nums[i - 1] < nums[i]; ++i)
      ;
    return check(nums, i - 1) || check(nums, i);
  }

  bool check(vector<int>& nums, int i) {
    int prev = 0;
    for (int j = 0; j < nums.size(); ++j) {
      if (i == j) continue;
      if (prev >= nums[j]) return false;
      prev = nums[j];
    }
    return true;
  }
};
func canBeIncreasing(nums []int) bool {
  i, n := 1, len(nums)
  for ; i < n && nums[i-1] < nums[i]; i++ {

  }
  return check(nums, i-1) || check(nums, i)
}

func check(nums []int, i int) bool {
  prev := 0
  for j := 0; j < len(nums); j++ {
    if i == j {
      continue
    }
    if prev >= nums[j] {
      return false
    }
    prev = nums[j]
  }
  return true
}
function canBeIncreasing(nums: number[]): boolean {
  const check = (p: number) => {
    let prev = undefined;
    for (let j = 0; j < nums.length; j++) {
      if (p != j) {
        if (prev !== undefined && prev >= nums[j]) {
          return false;
        }
        prev = nums[j];
      }
    }
    return true;
  };
  for (let i = 0; i < nums.length; i++) {
    if (nums[i - 1] >= nums[i]) {
      return check(i - 1) || check(i);
    }
  }
  return true;
}
impl Solution {
  pub fn can_be_increasing(nums: Vec<i32>) -> bool {
    let check = |p: usize| -> bool {
      let mut prev = None;
      for j in 0..nums.len() {
        if p != j {
          if let Some(value) = prev {
            if value >= nums[j] {
              return false;
            }
          }
          prev = Some(nums[j]);
        }
      }
      true
    };
    for i in 1..nums.len() {
      if nums[i - 1] >= nums[i] {
        return check(i - 1) || check(i);
      }
    }
    true
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文