Skip to content

1014. Best Sightseeing Pair 👍

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  int maxScoreSightseeingPair(vector<int>& A) {
    int ans = 0;
    int bestPrev = 0;

    for (int a : A) {
      ans = max(ans, a + bestPrev);
      bestPrev = max(bestPrev, a) - 1;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public int maxScoreSightseeingPair(int[] A) {
    int ans = 0;
    int bestPrev = 0;

    for (int a : A) {
      ans = Math.max(ans, a + bestPrev);
      bestPrev = Math.max(bestPrev, a) - 1;
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
  def maxScoreSightseeingPair(self, A: List[int]) -> int:
    ans = 0
    bestPrev = 0

    for a in A:
      ans = max(ans, a + bestPrev)
      bestPrev = max(bestPrev, a) - 1

    return ans
Back to top