AlgoMaster Logo

Best Sightseeing Pair

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force

Intuition:

The brute force solution simply involves calculating the score for each possible pair of indices (i, j) where i < j. The score for a pair is defined as A[i] + A[j] + i - j. We iterate through all possible pairs and compute this score, keeping track of the maximum score encountered.

Code:

2. Precomputation

Intuition:

This solution involves precomputing the maximum value of A[i] + i for each index, and then for each j, computing the maximum score using the precomputed values.

Code:

3. One Pass

Intuition:

The most efficient approach is to calculate A[i] + i on the go while iterating. We keep track of the maximum A[i] + i encountered so far and update the result with each A[j] - j. This allows us to solve the problem in a single pass with constant space.

Code: