/**
* Created by lxw, liwei4939@126.com on 2017/11/2.
* 需要排序的最短子数组的长度
*/
public class paramMinLength {
public int getMinLength(int[] arr){
if(arr == null || arr.length < 2){
return 0;
}
int min = arr[arr.length - 1];
int noMinIndex = -1;
for (int i= arr.length -2; i > -1; i--){
if(arr[i] > min){
noMinIndex = i;
}else {
min = Math.min(min, arr[i]);
}
}
if(noMinIndex == -1){
return 0;
}
int max = arr[0];
int noMaxIndex = -1;
for (int i=1; i< arr.length; i++){
if(arr[i] < max){
noMaxIndex = i;
} else {
max = Math.max(max, arr[i]);
}
}
return noMaxIndex - noMinIndex + 1;
}
public static void main(String[] args){
paramMinLength tmp = new paramMinLength();
int[] arr = {1, 5, 3, 4, 2, 6, 7};
System.out.println(tmp.getMinLength(arr));
}
}
需要排序的最短子数组的长度
最新推荐文章于 2022-11-07 22:28:50 发布