两种思路:第一种是滑动窗口,第二种是直接暴搜,考虑到字节平台的数据量问题,其实直接暴力就能ac,代码如下:
import java.util.Arrays;
public class Main {
public static int solution(int[] values) {
if(values.length==0||values==null||values.length==1){
return 0;
}
int score=Integer.MIN_VALUE;
for(int i=0;i<values.length-1;i++){
for(int j=i+1;j<values.length;j++){
score=Math.max(score,values[i]+values[j]+i-j );
}
}
return score; // Placeholder return
}
public static void main(String[] args) {
System.out.println(solution(new int[]{8, 3, 5, 5, 6}) == 11 ? 1 : 0);
System.out.println(solution(new int[]{10, 4, 8, 7}) == 16 ? 1 : 0);
System.out.println(solution(new int[]{1, 2, 3, 4, 5}) == 8 ? 1 : 0);
}
}