Opening the reading…
Opening the reading…
SLIDING WINDOW › FIXED SIZE SLIDING-WINDOW
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →Because the points are sorted by x and i < j, every valid pair has x_i < x_j. Therefore |x_i - x_j| is simply x_j - x_i. The expression becomes y_i + y_j + x_j - x_i, which can be regrouped as (y_i - x_i) + (x_j + y_j). For a fixed current point j, the second term is fixed, so only the best earlier value of y_i - x_i matters.
As j moves from left to right, the eligible earlier points form a sliding window: a point remains usable while x_j - x_i <= k, then expires forever. You need the maximum y_i - x_i inside that window, but you do not need to keep every candidate. If one point has a smaller or equal value and expires no later than another point with a larger value, it can never win a future query.
A deque stores the remaining candidates from largest y_i - x_i to smallest. Its front is the best candidate for the current point. Removing expired points from the front handles the window, while removing dominated points from the back preserves only values that could still become optimal.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) | Each point is pushed once, removed from the front at most once, and removed from the back at most once. Although a single iteration can perform many removals, those removals consume points that cannot be removed again, so the total number of deque operations is linear. |
| Space | O(n) extra | The deque can contain every point in the worst case, such as when candidate values decrease strictly and all points remain within range. The returned value is a scalar, so there is no output container to count; the deque is the only working storage. |
#include <algorithm>
#include <climits>
#include <deque>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
int findMaxValueOfEquation(vector<vector<int>>& points, int k) {
int answer = INT_MIN;
deque<pair<int, int>> candidates;
// Each entry is {y - x, x}.
for (const vector<int>& point : points) {
int x = point[0];
int y = point[1];
while (!candidates.empty() &&
x - candidates.front().second > k) {
candidates.pop_front();
}
if (!candidates.empty()) {
answer = max(answer, candidates.front().first + x + y);
}
int value = y - x;
while (!candidates.empty() &&
candidates.back().first <= value) {
candidates.pop_back();
}
candidates.push_back({value, x});
}
return answer;
}
};The order of the three main phases is essential. Expiration must happen before the query, or the front may describe a point outside the allowed distance. The query must happen before insertion, or the current point can pair with itself. Only after the query is the current point allowed to compete with earlier points.
The deque stores pairs rather than only candidate values because expiration depends on x. The front value answers the optimization query, while the stored x tells you whether that candidate has fallen outside the current window. Since x values are strictly increasing, once a point expires it stays expired for every later point.
A max-heap can also store every earlier pair as (y_i - x_i, x_i). Its top is the strongest candidate, and expired entries are removed lazily whenever they reach the top. This is a different arrangement, not an optimization: it is easier to recognize as a sliding-window maximum, but it pays logarithmic time per insertion and removal instead of using the deque's linear amortized time.
#include <algorithm>
#include <climits>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
int findMaxValueOfEquation(vector<vector<int>>& points, int k) {
int answer = INT_MIN;
priority_queue<pair<int, int>> candidates;
for (const vector<int>& point : points) {
int x = point[0];
int y = point[1];
while (!candidates.empty() &&
x - candidates.top().second > k) {
candidates.pop();
}
if (!candidates.empty()) {
answer = max(answer, candidates.top().first + x + y);
}
candidates.push({y - x, x});
}
return answer;
}
};