All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1001_1100.s1014_best_sightseeing_pair.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g1001_1100.s1014_best_sightseeing_pair;

// #Medium #Array #Dynamic_Programming #Dynamic_Programming_I_Day_7
// #2022_02_25_Time_2_ms_(99.86%)_Space_51.3_MB_(73.75%)

public class Solution {
    public int maxScoreSightseeingPair(int[] a) {
        int bestPrevious = a[0];
        int maxSum = 0;

        for (int i = 1; i < a.length; ++i) {
            maxSum = Math.max(maxSum, bestPrevious + a[i] - i);
            bestPrevious = Math.max(bestPrevious, a[i] + i);
        }

        return maxSum;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy