diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README.md b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README.md index ad7b37699704e..645694573be44 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README.md +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README.md @@ -73,7 +73,11 @@ tags: -### 方法一 +### 方法一:遍历 + +我们可以遍历数组,找到第一个不满足 $\textit{nums}[i] < \textit{nums}[i+1]$ 的位置 $i$,然后检查删除 $i$ 或者 $i+1$ 后的数组是否严格递增,如果是则返回 $\textit{true}$,否则返回 $\textit{false}$。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 @@ -82,20 +86,20 @@ tags: ```python class Solution: def canBeIncreasing(self, nums: List[int]) -> bool: - def check(nums, i): - prev = -inf - for j, num in enumerate(nums): - if i == j: + def check(k: int) -> bool: + pre = -inf + for i, x in enumerate(nums): + if i == k: continue - if prev >= nums[j]: + if pre >= x: return False - prev = nums[j] + pre = x return True - i, n = 1, len(nums) - while i < n and nums[i - 1] < nums[i]: + i = 0 + while i + 1 < len(nums) and nums[i] < nums[i + 1]: i += 1 - return check(nums, i - 1) or check(nums, i) + return check(i) or check(i + 1) ``` #### Java @@ -103,22 +107,23 @@ class Solution: ```java 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); + int i = 0; + while (i + 1 < nums.length && nums[i] < nums[i + 1]) { + ++i; + } + return check(nums, i) || check(nums, i + 1); } - private boolean check(int[] nums, int i) { - int prev = Integer.MIN_VALUE; - for (int j = 0; j < nums.length; ++j) { - if (i == j) { + private boolean check(int[] nums, int k) { + int pre = 0; + for (int i = 0; i < nums.length; ++i) { + if (i == k) { continue; } - if (prev >= nums[j]) { + if (pre >= nums[i]) { return false; } - prev = nums[j]; + pre = nums[i]; } return true; } @@ -131,20 +136,25 @@ class Solution { class Solution { public: bool canBeIncreasing(vector& 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& 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]; + int n = nums.size(); + auto check = [&](int k) -> bool { + int pre = 0; + for (int i = 0; i < n; ++i) { + if (i == k) { + continue; + } + if (pre >= nums[i]) { + return false; + } + pre = nums[i]; + } + return true; + }; + int i = 0; + while (i + 1 < n && nums[i] < nums[i + 1]) { + ++i; } - return true; + return check(i) || check(i + 1); } }; ``` @@ -153,25 +163,24 @@ public: ```go 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 + check := func(k int) bool { + pre := 0 + for i, x := range nums { + if i == k { + continue + } + if pre >= x { + return false + } + pre = x } - prev = nums[j] + return true } - return true + i := 0 + for i+1 < len(nums) && nums[i] < nums[i+1] { + i++ + } + return check(i) || check(i+1) } ``` @@ -179,24 +188,25 @@ func check(nums []int, i int) bool { ```ts 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]; + const n = nums.length; + const check = (k: number): boolean => { + let pre = 0; + for (let i = 0; i < n; ++i) { + if (i === k) { + continue; } + if (pre >= nums[i]) { + return false; + } + pre = nums[i]; } return true; }; - for (let i = 0; i < nums.length; i++) { - if (nums[i - 1] >= nums[i]) { - return check(i - 1) || check(i); - } + let i = 0; + while (i + 1 < n && nums[i] < nums[i + 1]) { + ++i; } - return true; + return check(i) || check(i + 1); } ``` @@ -205,26 +215,53 @@ function canBeIncreasing(nums: number[]): boolean { ```rust impl Solution { pub fn can_be_increasing(nums: Vec) -> 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]); + let check = |k: usize| -> bool { + let mut pre = 0; + for (i, &x) in nums.iter().enumerate() { + if i == k { + continue; } + if pre >= x { + return false; + } + pre = x; } true }; - for i in 1..nums.len() { - if nums[i - 1] >= nums[i] { - return check(i - 1) || check(i); + + let mut i = 0; + while i + 1 < nums.len() && nums[i] < nums[i + 1] { + i += 1; + } + check(i) || check(i + 1) + } +} +``` + +#### C# + +```cs +public class Solution { + public bool CanBeIncreasing(int[] nums) { + int n = nums.Length; + bool check(int k) { + int pre = 0; + for (int i = 0; i < n; ++i) { + if (i == k) { + continue; + } + if (pre >= nums[i]) { + return false; + } + pre = nums[i]; } + return true; + } + int i = 0; + while (i + 1 < n && nums[i] < nums[i + 1]) { + ++i; } - true + return check(i) || check(i + 1); } } ``` diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README_EN.md b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README_EN.md index a91056b311766..e9ccc6a2d9ec7 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README_EN.md +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README_EN.md @@ -67,7 +67,11 @@ No resulting array is strictly increasing, so return false. -### Solution 1 +### Solution 1: Traversal + +We can traverse the array to find the first position $i$ where $\textit{nums}[i] < \textit{nums}[i+1]$ is not satisfied. Then, we check if the array is strictly increasing after removing either $i$ or $i+1$. If it is, we return $\textit{true}$; otherwise, we return $\textit{false}$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. @@ -76,20 +80,20 @@ No resulting array is strictly increasing, so return false. ```python class Solution: def canBeIncreasing(self, nums: List[int]) -> bool: - def check(nums, i): - prev = -inf - for j, num in enumerate(nums): - if i == j: + def check(k: int) -> bool: + pre = -inf + for i, x in enumerate(nums): + if i == k: continue - if prev >= nums[j]: + if pre >= x: return False - prev = nums[j] + pre = x return True - i, n = 1, len(nums) - while i < n and nums[i - 1] < nums[i]: + i = 0 + while i + 1 < len(nums) and nums[i] < nums[i + 1]: i += 1 - return check(nums, i - 1) or check(nums, i) + return check(i) or check(i + 1) ``` #### Java @@ -97,22 +101,23 @@ class Solution: ```java 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); + int i = 0; + while (i + 1 < nums.length && nums[i] < nums[i + 1]) { + ++i; + } + return check(nums, i) || check(nums, i + 1); } - private boolean check(int[] nums, int i) { - int prev = Integer.MIN_VALUE; - for (int j = 0; j < nums.length; ++j) { - if (i == j) { + private boolean check(int[] nums, int k) { + int pre = 0; + for (int i = 0; i < nums.length; ++i) { + if (i == k) { continue; } - if (prev >= nums[j]) { + if (pre >= nums[i]) { return false; } - prev = nums[j]; + pre = nums[i]; } return true; } @@ -125,20 +130,25 @@ class Solution { class Solution { public: bool canBeIncreasing(vector& 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& 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]; + int n = nums.size(); + auto check = [&](int k) -> bool { + int pre = 0; + for (int i = 0; i < n; ++i) { + if (i == k) { + continue; + } + if (pre >= nums[i]) { + return false; + } + pre = nums[i]; + } + return true; + }; + int i = 0; + while (i + 1 < n && nums[i] < nums[i + 1]) { + ++i; } - return true; + return check(i) || check(i + 1); } }; ``` @@ -147,25 +157,24 @@ public: ```go 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 + check := func(k int) bool { + pre := 0 + for i, x := range nums { + if i == k { + continue + } + if pre >= x { + return false + } + pre = x } - prev = nums[j] + return true } - return true + i := 0 + for i+1 < len(nums) && nums[i] < nums[i+1] { + i++ + } + return check(i) || check(i+1) } ``` @@ -173,24 +182,25 @@ func check(nums []int, i int) bool { ```ts 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]; + const n = nums.length; + const check = (k: number): boolean => { + let pre = 0; + for (let i = 0; i < n; ++i) { + if (i === k) { + continue; } + if (pre >= nums[i]) { + return false; + } + pre = nums[i]; } return true; }; - for (let i = 0; i < nums.length; i++) { - if (nums[i - 1] >= nums[i]) { - return check(i - 1) || check(i); - } + let i = 0; + while (i + 1 < n && nums[i] < nums[i + 1]) { + ++i; } - return true; + return check(i) || check(i + 1); } ``` @@ -199,26 +209,53 @@ function canBeIncreasing(nums: number[]): boolean { ```rust impl Solution { pub fn can_be_increasing(nums: Vec) -> 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]); + let check = |k: usize| -> bool { + let mut pre = 0; + for (i, &x) in nums.iter().enumerate() { + if i == k { + continue; } + if pre >= x { + return false; + } + pre = x; } true }; - for i in 1..nums.len() { - if nums[i - 1] >= nums[i] { - return check(i - 1) || check(i); + + let mut i = 0; + while i + 1 < nums.len() && nums[i] < nums[i + 1] { + i += 1; + } + check(i) || check(i + 1) + } +} +``` + +#### C# + +```cs +public class Solution { + public bool CanBeIncreasing(int[] nums) { + int n = nums.Length; + bool check(int k) { + int pre = 0; + for (int i = 0; i < n; ++i) { + if (i == k) { + continue; + } + if (pre >= nums[i]) { + return false; + } + pre = nums[i]; } + return true; + } + int i = 0; + while (i + 1 < n && nums[i] < nums[i + 1]) { + ++i; } - true + return check(i) || check(i + 1); } } ``` diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.cpp b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.cpp index d556f8519c213..e9574d6175483 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.cpp +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.cpp @@ -1,19 +1,24 @@ class Solution { public: bool canBeIncreasing(vector& 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& 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]; + int n = nums.size(); + auto check = [&](int k) -> bool { + int pre = 0; + for (int i = 0; i < n; ++i) { + if (i == k) { + continue; + } + if (pre >= nums[i]) { + return false; + } + pre = nums[i]; + } + return true; + }; + int i = 0; + while (i + 1 < n && nums[i] < nums[i + 1]) { + ++i; } - return true; + return check(i) || check(i + 1); } -}; \ No newline at end of file +}; diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.cs b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.cs new file mode 100644 index 0000000000000..9bc16e2fcbe5f --- /dev/null +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.cs @@ -0,0 +1,23 @@ +public class Solution { + public bool CanBeIncreasing(int[] nums) { + int n = nums.Length; + bool check(int k) { + int pre = 0; + for (int i = 0; i < n; ++i) { + if (i == k) { + continue; + } + if (pre >= nums[i]) { + return false; + } + pre = nums[i]; + } + return true; + } + int i = 0; + while (i + 1 < n && nums[i] < nums[i + 1]) { + ++i; + } + return check(i) || check(i + 1); + } +} diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.go b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.go index 1ab8e75ada612..df5fa661d0536 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.go +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.go @@ -1,21 +1,20 @@ 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 + check := func(k int) bool { + pre := 0 + for i, x := range nums { + if i == k { + continue + } + if pre >= x { + return false + } + pre = x } - prev = nums[j] + return true } - return true -} \ No newline at end of file + i := 0 + for i+1 < len(nums) && nums[i] < nums[i+1] { + i++ + } + return check(i) || check(i+1) +} diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.java b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.java index 528aa13e423cd..b22193788762b 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.java +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.java @@ -1,22 +1,23 @@ 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); + int i = 0; + while (i + 1 < nums.length && nums[i] < nums[i + 1]) { + ++i; + } + return check(nums, i) || check(nums, i + 1); } - private boolean check(int[] nums, int i) { - int prev = Integer.MIN_VALUE; - for (int j = 0; j < nums.length; ++j) { - if (i == j) { + private boolean check(int[] nums, int k) { + int pre = 0; + for (int i = 0; i < nums.length; ++i) { + if (i == k) { continue; } - if (prev >= nums[j]) { + if (pre >= nums[i]) { return false; } - prev = nums[j]; + pre = nums[i]; } return true; } -} \ No newline at end of file +} diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.py b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.py index 2cab1a155dfb6..bf744eb161540 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.py +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.py @@ -1,16 +1,16 @@ class Solution: def canBeIncreasing(self, nums: List[int]) -> bool: - def check(nums, i): - prev = -inf - for j, num in enumerate(nums): - if i == j: + def check(k: int) -> bool: + pre = -inf + for i, x in enumerate(nums): + if i == k: continue - if prev >= nums[j]: + if pre >= x: return False - prev = nums[j] + pre = x return True - i, n = 1, len(nums) - while i < n and nums[i - 1] < nums[i]: + i = 0 + while i + 1 < len(nums) and nums[i] < nums[i + 1]: i += 1 - return check(nums, i - 1) or check(nums, i) + return check(i) or check(i + 1) diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.rs b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.rs index 0908b1f88652b..2fc9b46f336a2 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.rs +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.rs @@ -1,24 +1,23 @@ impl Solution { pub fn can_be_increasing(nums: Vec) -> 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]); + let check = |k: usize| -> bool { + let mut pre = 0; + for (i, &x) in nums.iter().enumerate() { + if i == k { + continue; } + if pre >= x { + return false; + } + pre = x; } true }; - for i in 1..nums.len() { - if nums[i - 1] >= nums[i] { - return check(i - 1) || check(i); - } + + let mut i = 0; + while i + 1 < nums.len() && nums[i] < nums[i + 1] { + i += 1; } - true + check(i) || check(i + 1) } } diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.ts b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.ts index d81c5a9c7726b..8abc93b1d4797 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.ts +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.ts @@ -1,20 +1,21 @@ 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]; + const n = nums.length; + const check = (k: number): boolean => { + let pre = 0; + for (let i = 0; i < n; ++i) { + if (i === k) { + continue; } + if (pre >= nums[i]) { + return false; + } + pre = nums[i]; } return true; }; - for (let i = 0; i < nums.length; i++) { - if (nums[i - 1] >= nums[i]) { - return check(i - 1) || check(i); - } + let i = 0; + while (i + 1 < n && nums[i] < nums[i + 1]) { + ++i; } - return true; + return check(i) || check(i + 1); }