阿里0414笔试第一题序列的密度集,只有30%的通过率,有大佬知道哪里有问题吗?
import java.util.*;
public class K {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
int[] out = new int[n+1];
for (int i = 1; i<= n; i++) {
int left = 0;
int right = 0;
int len = 0;
while(left <= right && right < n){
if (nums[right] >= i) {
right++;
len = right - left;
}else {
left = ++right;
len = 0;
}
if (len == i) {
break;
}
}
out[i] = len;
}
for (int i = n; i > 0; i--) {
if(out[i] == i){
System.out.println(i);
return;
}
}
}
}
全部评论
(1) 回帖