LeetCode Solutions

1014. Best Sightseeing Pair

Time:

Space:

			

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;
  }
};
			

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;
  }
}
			

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