Skip to content

Latest commit

 

History

History
 
 

1499. Max Value of Equation

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Given an array points containing the coordinates of points on a 2D plane, sorted by the x-values, where points[i] = [xi, yi] such that xi < xj for all 1 <= i < j <= points.length. You are also given an integer k.

Find the maximum value of the equation yi + yj + |xi - xj| where |xi - xj| <= k and 1 <= i < j <= points.length. It is guaranteed that there exists at least one pair of points that satisfy the constraint |xi - xj| <= k.

 

Example 1:

Input: points = [[1,3],[2,0],[5,10],[6,-10]], k = 1
Output: 4
Explanation: The first two points satisfy the condition |xi - xj| <= 1 and if we calculate the equation we get 3 + 0 + |1 - 2| = 4. Third and fourth points also satisfy the condition and give a value of 10 + -10 + |5 - 6| = 1.
No other pairs satisfy the condition, so we return the max of 4 and 1.

Example 2:

Input: points = [[0,0],[3,0],[9,2]], k = 3
Output: 3
Explanation: Only the first two points have an absolute difference of 3 or less in the x-values, and give the value of 0 + 0 + |0 - 3| = 3.

 

Constraints:

  • 2 <= points.length <= 10^5
  • points[i].length == 2
  • -10^8 <= points[i][0], points[i][1] <= 10^8
  • 0 <= k <= 2 * 10^8
  • points[i][0] < points[j][0] for all 1 <= i < j <= points.length
  • xi form a strictly increasing sequence.

Related Topics:
Array, Sliding Window

Solution 1. Multiset

For the equation yi + yj + |xi - xj|, since j > i, so xj must be greater than xi, so the equation is the same as yi + yj + xj - xi = xj + yj - xi + yi. For a given i, -xi + yi is a constant, so we just need to find the maximum xj + yj satisfying the k constraint.

Keep a sliding window [i, j). The elements in the window satisfy the k constraint. Use a multiset<int> s to keep the x + y values in the window except for that for the A[i].

For this A[i], the maximum value we can get is A[i][1] - A[i][0] plus the maximum value in the multiset.

// OJ: https://leetcode.com/problems/max-value-of-equation/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
    int findMaxValueOfEquation(vector<vector<int>>& A, int k) {
        int i = 0, j = 0, N = A.size(), ans = INT_MIN;
        multiset<int> s;
        for (; i < N; ++i) {
            for (; j < N && A[j][0] - A[i][0] <= k; ++j) s.insert(A[j][0] + A[j][1]);
            s.erase(s.find(A[i][0] + A[i][1]));
            if (s.size()) ans = max(ans, A[i][1] - A[i][0] + *s.rbegin());
        }
        return ans;
    }
};

Solution 2. Monoqueue

Since we only care about the maximum value in a sliding window, we can use a descending monoqueue to keep track of the maximum value.

// OJ: https://leetcode.com/problems/max-value-of-equation/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
    int findMaxValueOfEquation(vector<vector<int>>& A, int k) {
        int i = 0, j = 0, N = A.size(), ans = INT_MIN;
        deque<int> q; // descending monoqueue
        for (; i < N; ++i) {
            for (; j < N && A[j][0] - A[i][0] <= k; ++j) {
                int sum = A[j][0] + A[j][1];
                while (q.size() && q.back() < sum) q.pop_back();
                q.push_back(sum);
            }
            if (q.size() && q.front() == A[i][0] + A[i][1]) q.pop_front();
            if (q.size()) ans = max(ans, A[i][1] - A[i][0] + q.front());
        }
        return ans;
    }
};