classSolution {publicintmaxScoreSightseeingPair(int[] A) {int res =0;int preMax =A[0] +0; //preMax = A[i] + i;for (int j=1;j<A.length;j++) {// res = A[i] + A[j] + i - j// res = A[i] + i + A[j] - j// res = preMax + (A[j] - j) res =Math.max(res, preMax + (A[j] - j));//不断更新最大结果// 更新j前面最大的A[i] + i preMax =Math.max(preMax,A[j] + j); //这个就是题中的A[i]+i }return res; }}
2.2 Kotlin
importkotlin.math.maxclass Solution { fun maxScoreSightseeingPair(A: IntArray): Int { var res = 0 var preMax = A[0] + 0 //preMax = A[i] + i for (j in 1..A.size-1) {// res = A[i] + A[j] + i - j// res = A[i] + i + A[j] - j// res = preMax + (A[j] - j) res = max(res, preMax + (A[j] - j))//不断更新最大结果// 更新j前面最大的A[i] + i preMax = max(preMax, A[j] + j) //这个就是题中的A[i]+i } return res }}