Java 2ms 99% time speed, simple code| explained
class Solution {
    public int maxScoreSightseeingPair(int[] n) {
        int ans=0, max=n[0], in=0;
        for(int i=1 ; i<n.length ; i++)
        {
			// if the number and its index is greater than the previous max and index 
			// than store it in ans
            if(max+n[i]+in-i>=ans) ans=max+n[i]+in-i;
			
			// important thing here the previous max number is not the the greater element
			// but sum of element and index , so if the sum of  current element and index is 
			// greater than the previous element and index than store the current element and 
			// and index as max value
            if(n[i]+i>=max+in)
            {
                max=n[i];                    
                in=i;
            }
        }
        return ans;
    }
}
Comments (0)
No comments yet.