网易笔试牛牛找工作,java代码通过40%,求解该如何优化
一直时间复杂度太高,求解该如何优化
import java.math.BigInteger;
import java.util.Scanner;public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
BigInteger[] di = new BigInteger[n];
BigInteger[] pi = new BigInteger[n];
BigInteger[] ai = new BigInteger[m];
for (int i = 0; i < n; i++) {
di[i] = sc.nextBigInteger();
pi[i] = sc.nextBigInteger();
}
for (int i = 0; i < m; i++) {
ai[i] = sc.nextBigInteger();
}
for (BigInteger bigInteger : ai) {
BigInteger MaxSalary = getMaxSalary(bigInteger, di, pi);
System.out.println(MaxSalary);
}
}
public static BigInteger getMaxSalary(BigInteger ai,BigInteger[] di,BigInteger[] pi) {
BigInteger MaxSalary = BigInteger.ZERO;
for (int i = 0; i < di.length; i++) {
if (ai.compareTo(di[i]) >= 0 && MaxSalary.compareTo(pi[i]) < 0) {
MaxSalary = pi[i];
}
}
return MaxSalary;
}
}
#春招##笔试题目##实习#