Best Sightseeing Pair

MediumDynamic Programminggreedy

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

values[i] is the rating of the i-th sightseeing spot along a road; spots i and j are j - i units apart. Visiting a pair of spots i < j gives a score of values[i] + values[j] + i - j (the ratings minus the distance between them). Return the maximum score over all pairs i < j. Aim for a single pass.

Examples

Input: values = [8,1,5,2,6]
Output: 11
i = 0, j = 2: 8 + 5 + 0 - 2 = 11.
Input: values = [1,2]
Output: 2
The only pair: 1 + 2 + 0 - 1 = 2.

Constraints

  • 2 <= values.length <= 5 * 10^4
  • 1 <= values[i] <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More greedy problems