75-days-dsa-challenge

Ninja technique🥷 to ACE DSA Interviews.

View the Project on GitHub

1499. Max Value of Equation

Max heap solution

Code

class Solution {
public:
    int findMaxValueOfEquation(vector<vector<int>>& points, int k)
    {
        priority_queue<pair<int, int>> pq;
        int ans = INT_MIN;
        int n = points.size();
        for (int i = 0; i < n; i++) {
            // if (xj - xi > k) pop points
            while ((!pq.empty()) && (points[i][0] - pq.top().second > k))
                pq.pop();
            if (!pq.empty()) {
                // maximize the value of equation yi + yj + |xi - xj| = (xj + yj) + (yi - xi)
                // xj and yj are current points i.e. constant (xj + yj)
                // if we can maximize (yi - xi) our equation will automatically become maximum
                // ans = max(ans, xj + yj + pq.top().first[prev max(yi-xi)]);
                ans = max(ans, points[i][0] + points[i][1] + pq.top().first);
            }
            // add new points {yi - xi, xi}
            pq.push({ points[i][1] - points[i][0], points[i][0] });
        }
        return ans;
    }
};