华为第一题 日志处理 前缀和解法100%通过耗时747ms
自认为最快,希望评论区大神点评下😂
#华为#
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int length = sc.nextInt();
long[] records = new long[length];
long[] preSum = new long[length];
for (int i = 0; i < records.length; i++) {
records[i] = sc.nextLong();
}
long total = sc.nextLong();
long sum = 0;
Arrays.sort(records);
for (int i = 0; i < records.length; i++) {
preSum[i] = sum;
sum += records[i];
}
if (sum <= total) {
System.out.println(-1);
} else {
long[] temp = new long[length];
long result = 0;
for (int i = 0; i < temp.length; i++) {
temp[i] = preSum[i] + records[i] * (records.length - i);
}
for (int i = 0; i < temp.length; i++) {
if(temp[0]>total){
result=total/temp.length;
break;
}
if (temp[i] <= total && temp[i + 1] > total) {
long res = (total - temp[i]) / (temp.length - i - 1);
result = records[i] + res;
break;
}
}
System.out.println(result);
}
}
} #华为#
查看7道真题和解析