Fully explained, Python Easy to understand, Linear time with constant space code
See you only want to find the maximum value of ((val[i] + i) + (val[j]-j)) which is easy if we have our maximum value of (val[i] + i) being precalculated at eve
See you only want to find the maximum value of ((val[i] + i) + (val[j]-j)) which is easy if we have our maximum value of (val[i] + i) being precalculated at every index. So just add (val[j]-j) to that at that "j".\nHere is a simple 8 line code for the same. \nFor better undestanding please take a simple test case and go through each and every line in the code. If you like the solution, please like and upvote it, so it can help other young coders like you. \nThankYou and Have a nice day.\n```\nclass Solution:\n    def maxScoreSightseeingPair(self, values) -> int:\n        maxAns = float("-inf") \n        # precalculating the i value\n        currPrevMax = values[0]+0\n        for j in range(1,len(values)):\n            maxAns = max(maxAns,currPrevMax+values[j]-j)\n            # Update the previous max incase the current j is the new i\n            currPrevMax = max(currPrevMax, values[j]+j) \n        return maxAns\n
['Dynamic Programming', 'Python']