[Leetcode Learning-c ++ & java] 최단 정렬되지 않은 연속 하위 배열 (최단 정렬 거리 찾기)

문제:

난이도 : 중간

기술:

배열이 주어지면 오름차순이 아닌 하위 시퀀스를 작은 것에서 큰 것으로 찾은 다음, 오름차순 하위 시퀀스가 ​​아닌 정렬해야하는 모든 하위 문자열 (연속적인 하위 시퀀스)을 포함하는 가장 짧은 하위 시퀀스를 계산합니다.

제목 링크 : https://leetcode.com/problems/shortest-unsorted-continuous-subarray/

입력 범위 :

  1. 1 <= nums.length <= 104
  2. -105 <= nums[i] <= 105

케이스 입력 :

Example 1:  比如说 6, 4, 8, 10, 9 这一大串有两个不连续的非升序子序列 [6, 4] [9, 10]
Input: nums = [2,6,4,8,10,9,15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.

Example 2:
Input: nums = [1,2,3,4]
Output: 0

Example 3:
Input: nums = [1]
Output: 0

내 코드 :

정렬은 다음과 같을 수 있기 때문입니다.

1,3,5,4,2 很小的 2 放到非常后面,需要往前找比它小的元素的位置
1,9,5,4,2 很大的 9 放到很前面,需要往后找比它大的元素的位置。
1,3,2,2,2 很多重复的元素放到一起,可能是小很多的,可能是大很多的 如:1,9,9,9,2,2,2

따라서 첫 번째 비 오름 위치 시작과 마지막 비 오름 위치 끝을 찾은 다음 모든 비 오름 위치의 최대 최대 및 최소 최소를 계산하고 시작에서 최소 위치를 찾은 다음 다음에서 최대를 찾아야합니다. end 위치로 인해 두 위치의 뺄셈은 정렬해야하는 길이입니다.

자바:

class Solution {
    public int findUnsortedSubarray(int[] nums) {
        boolean flag = true;
        int left = 0, minL = Integer.MAX_VALUE, preR = 0, maxR = Integer.MIN_VALUE, cr = 0, right = 1, len = nums.length;
        while(right != len) {
            if(nums[right] < nums[preR]) {
                if(flag) left = right - 1;
                while(right + 1 < len && nums[right] == nums[right + 1]) right ++;
                if(flag) {
                    maxR = nums[preR];
                    while(left >= 0 && nums[right] < nums[left]) left --;
                    left ++; flag = false;
                }
                cr = right;
                maxR = Math.max(nums[preR], maxR);
                minL = Math.min(nums[right], minL);
            }
            preR = right;
            right ++;
        }
        while(cr < len && maxR > nums[cr]) cr ++;
        while(left >= 0 && minL < nums[left]) left --;
        return cr - left == 0 ? 0 : cr - left - 1;
    }
}

C ++ :

class Solution {
public:
    int findUnsortedSubarray(vector<int>& nums) {
        bool flag = true;
        int left = 0, minL = INT_MAX, preR = 0, maxR = INT_MIN, cr = 0, right = 1, len = nums.size();
        while(right != len) {
            if(nums[right] < nums[preR]) {
                if(flag) left = right - 1;
                while(right + 1 < len && nums[right] == nums[right + 1]) right ++;
                if(flag) {
                    maxR = nums[preR];
                    while(left >= 0 && nums[right] < nums[left]) left --;
                    left ++; flag = false;
                }
                cr = right;
                maxR = max(nums[preR], maxR);
                minL = min(nums[right], minL);
            }
            preR = right;
            right ++;
        }
        while(cr < len && maxR > nums[cr]) cr ++;
        while(left >= 0 && minL < nums[left]) left --;
        return cr - left == 0 ? 0 : cr - left - 1;
    }
};

 

 

 

추천

출처blog.csdn.net/qq_28033719/article/details/114115920