classSolution{public:intmaxScoreSightseeingPair(vector<int>&values){intn=values.size();intmaxPair=0;// It will store our maximum score so far// It will start from the first value till second last// Why? we need a value after `i` for making a pairfor(inti=0;i<n-1;i++){intcurrentPair=0;// It will store our current score of each pair// It will start from after `i` till end of the arrayfor(intj=i+1;j<n;j++){// Calculate the pair using the formulacurrentPair=values[i]+values[j]+i-j;// Whichever is the maximum, take itmaxPair=max(maxPair,currentPair);}}returnmaxPair;}};
classSolution{public:intmaxScoreSightseeingPair(vector<int>&values){intn=values.size();// Ye har `j` ke pehle jo bhi maximum Part A (values[i] + i) ko store karenga// We intialize it to first value of our values arrayintprevMax=values[0]+0;intmaxPair=0;// Ye sab se badi pair value ko store karenga// Ek loop chalayenge j = 1, se end takfor(intj=1;j<n;j++){// har `j` ka current pair store karenga// isme ham Part A + Part B ko store karengeintcurrentPair=prevMax+(values[j]-j);// Update maxPair, whichever is bigmaxPair=max(maxPair,currentPair);// Aage jo `j` aayenga uske best Part A ko find karengaprevMax=max(prevMax,values[j]+j);}returnmaxPair;}};
Warning
LINK
You are about to visit a link which has been flagged with the above content warnings. Do you wish to continue?