Summary of Leetcode Solutions 1574. Delete the shortest sub-array to make the remaining arrays orderly

Directory link:

Lituo Programming Problems - Summary of Solutions_Share+Records-CSDN Blog

GitHub synchronous brushing project:

https://github.com/September26/java-algorithms

Link to the original title: Likou


describe:

Given an array of integers  arr , please delete a subarray (which can be empty) so that  arr the remaining elements in it are non- decreasing  .

A subarray refers to a contiguous subsequence of the original array.

Please return the length of the shortest subarray that meets the requirements of the question.

Example 1:

Input: arr = [1,2,3,10,4,2,3,5]
 Output: 3
 Explanation: The shortest subarray we need to delete is [10,4,2] with length 3. The remaining elements form the non-decreasing array [1,2,3,3,5].
Another correct solution is to delete the subarray [3,10,4].

Example 2:

Input: arr = [5,4,3,2,1]
 Output: 4
 Explanation: Since arrays are strictly decreasing, we can only keep one element. So we need to delete subarrays of length 4, either [5,4,3,2] or [4,3,2,1].

Example 3:

Input: arr = [1,2,3]
 Output: 0
 Explanation: The array is already non-decreasing, we don't need to delete any elements.

Example 4:

Input: arr = [1]
 Output: 0

hint:

  • 1 <= arr.length <= 10^5
  • 0 <= arr[i] <= 10^9

Problem-solving ideas:

* Problem-solving ideas:
* Divide the area into three pieces.
* The first block, counting from the beginning, the non-decreasing area.
* The second block, counting from the end, is a non-decreasing area.
* The third block is the remaining area.
* Because the first block is non-decreasing, we start traversing. We set the position to be i, and then find the first number in the second area that is greater than or equal to this i. We set its position to j, and the distance between i and j is The subsequence to be subtracted.
* In this way, the first area is traversed to find the minimum subsequence length, and compared with (total length - first block length) and (total length - second block length) to find the minimum value.
 
 

code:

public class Solution1574 {

    public int findLengthOfShortestSubarray(int[] arr) {
        int length = arr.length;
        int j = length - 1;
        while (j >= 1 && arr[j - 1] <= arr[j]) {
            j--;
        }
        //第一块的最小值。
        int start = 0;
        while (arr[start + 1] >= arr[start] && start < length - 2) {
            start++;
        }

        if (j == 0) {
            return 0;
        }
        //第二块的最小值。
        int abs = j;
        int i = 0;
        while (i < j && j <= length - 1) {
            if (arr[i] <= arr[j]) {
                abs = Math.min(abs, j - i - 1);
                if (arr[i] > arr[i + 1]) {
                    break;
                }
                i++;
                continue;
            }
            j++;
        }
        return Math.min(abs, length - start - 1);
    }
}

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/129822359