|
| 1 | +/** |
| 2 | + * [1909] Remove One Element to Make the Array Strictly Increasing |
| 3 | + * |
| 4 | + * 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. |
| 5 | + * The array nums is strictly increasing if nums[i - 1] < nums[i] for each index (1 <= i < nums.length). |
| 6 | + * |
| 7 | + * Example 1: |
| 8 | + * |
| 9 | + * Input: nums = [1,2,<u>10</u>,5,7] |
| 10 | + * Output: true |
| 11 | + * Explanation: By removing 10 at index 2 from nums, it becomes [1,2,5,7]. |
| 12 | + * [1,2,5,7] is strictly increasing, so return true. |
| 13 | + * |
| 14 | + * Example 2: |
| 15 | + * |
| 16 | + * Input: nums = [2,3,1,2] |
| 17 | + * Output: false |
| 18 | + * Explanation: |
| 19 | + * [3,1,2] is the result of removing the element at index 0. |
| 20 | + * [2,1,2] is the result of removing the element at index 1. |
| 21 | + * [2,3,2] is the result of removing the element at index 2. |
| 22 | + * [2,3,1] is the result of removing the element at index 3. |
| 23 | + * No resulting array is strictly increasing, so return false. |
| 24 | + * Example 3: |
| 25 | + * |
| 26 | + * Input: nums = [1,1,1] |
| 27 | + * Output: false |
| 28 | + * Explanation: The result of removing any element is [1,1]. |
| 29 | + * [1,1] is not strictly increasing, so return false. |
| 30 | + * |
| 31 | + * |
| 32 | + * Constraints: |
| 33 | + * |
| 34 | + * 2 <= nums.length <= 1000 |
| 35 | + * 1 <= nums[i] <= 1000 |
| 36 | + * |
| 37 | + */ |
| 38 | +pub struct Solution {} |
| 39 | + |
| 40 | +// problem: https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing/ |
| 41 | +// discuss: https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing/discuss/?currentPage=1&orderBy=most_votes&query= |
| 42 | + |
| 43 | +// submission codes start here |
| 44 | + |
| 45 | +impl Solution { |
| 46 | + pub fn can_be_increasing(nums: Vec<i32>) -> bool { |
| 47 | + let (mut is_dropped, mut prev_min) = (false, nums[0]); |
| 48 | + |
| 49 | + for (i, &num) in nums.iter().enumerate().skip(1) { |
| 50 | + match num <= prev_min { |
| 51 | + true if is_dropped => return false, |
| 52 | + true => { |
| 53 | + is_dropped = true; |
| 54 | + if i == 1 || num > nums[i - 2] { |
| 55 | + prev_min = num; |
| 56 | + } |
| 57 | + } |
| 58 | + false => { |
| 59 | + prev_min = num; |
| 60 | + } |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + true |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// submission codes end |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::*; |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn test_1909_example_1() { |
| 76 | + let nums = vec![1, 2, 10, 5, 7]; |
| 77 | + |
| 78 | + let result = true; |
| 79 | + |
| 80 | + assert_eq!(Solution::can_be_increasing(nums), result); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_1909_example_2() { |
| 85 | + let nums = vec![2, 3, 1, 2]; |
| 86 | + |
| 87 | + let result = false; |
| 88 | + |
| 89 | + assert_eq!(Solution::can_be_increasing(nums), result); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_1909_example_3() { |
| 94 | + let nums = vec![1, 1, 1]; |
| 95 | + |
| 96 | + let result = false; |
| 97 | + |
| 98 | + assert_eq!(Solution::can_be_increasing(nums), result); |
| 99 | + } |
| 100 | +} |
0 commit comments