程序员面试金典-面试题 16.16. 部分排序

题目:

给定一个整数数组,编写一个函数,找出索引m和n,只要将索引区间[m,n]的元素排好序,整个数组就是有序的。注意:n-m尽量最小,也就是说,找出符合条件的最短序列。函数返回值为[m,n],若不存在这样的m和n(例如整个数组是有序的),请返回[-1,-1]。

示例:

输入: [1,2,4,7,10,11,7,12,6,7,16,18,19]
输出: [3,9]
提示:

0 <= len(array) <= 1000000

分析:

以升序为例,我们要保证后面的元素比前面的元素都大,从头遍历数组,维护一个最大值变量,每遍历一个元素就更新最大值,同时如果当前元素小于最大值,就更新此时的索引。

 [1,2,4,7,10,11,7,12,6,7,16,18,19]以此为例,当遍历到7时,此时最大值是12,那么我们就将索引更新为7所指的索引,也就是需要排序的最短序列的右边界。

同理维护最小值求出左边界。

程序:

class Solution {
    public int[] subSort(int[] array) {
        int res0 = -1, res1 = -1;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < array.length; ++i){
            if(array[i] >= max){
                max = array[i];
            }else{
                res0 = i;
            }
        }
        for(int i = array.length - 1; i >= 0; --i){
            if(array[i] <= min){
                min = array[i];
            }else{
                res1 = i;
            }
        }
        return new int[]{res1, res0};
    }
}

猜你喜欢

转载自www.cnblogs.com/silentteller/p/12503944.html