You are 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.
Return the maximum value of the equationyi + 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.
Input: points =[[1,3],[2,0],[5,10],[6,-10]], k =1Output: 4Explanation: 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.
Input: points =[[0,0],[3,0],[9,2]], k =3Output: 3Explanation: 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.
The equation can be rewritten as (yi + xi) + (yj - xj) for i < j and |xi - xj| <= k. Since the points are sorted by xi, we can use a deque to maintain the maximum value of yi - xi for all valid i within the window of |xi - xj| <= k as we iterate through j.
classSolution {
public:int findMaxValueOfEquation(vector<vector<int>>& points, int k) {
deque<pair<int, int>> dq;
int ans = INT_MIN;
for (auto& pt : points) {
int x = pt[0], y = pt[1];
while (!dq.empty() && x - dq.front().second > k) dq.pop_front();
if (!dq.empty()) ans = max(ans, y + x + dq.front().first);
while (!dq.empty() && dq.back().first <= y - x) dq.pop_back();
dq.emplace_back(y - x, x);
}
return ans;
}
};
funcfindMaxValueOfEquation(points [][]int, kint) int {
typepairstruct{v, xint}
dq:= []pair{}
ans:=math.MinInt32for_, pt:=rangepoints {
x, y:=pt[0], pt[1]
for len(dq) > 0&&x-dq[0].x > k {
dq = dq[1:]
}
if len(dq) > 0 {
ify+x+dq[0].v > ans {
ans = y+x+dq[0].v }
}
for len(dq) > 0&&dq[len(dq)-1].v<=y-x {
dq = dq[:len(dq)-1]
}
dq = append(dq, pair{y-x, x})
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
publicintfindMaxValueOfEquation(int[][] points, int k) {
Deque<int[]> dq =new ArrayDeque<>();
int ans = Integer.MIN_VALUE;
for (int[] pt : points) {
int x = pt[0], y = pt[1];
while (!dq.isEmpty() && x - dq.peekFirst()[1]> k) dq.pollFirst();
if (!dq.isEmpty()) ans = Math.max(ans, y + x + dq.peekFirst()[0]);
while (!dq.isEmpty() && dq.peekLast()[0]<= y - x) dq.pollLast();
dq.offerLast(newint[]{y - x, x});
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funfindMaxValueOfEquation(points: Array<IntArray>, k: Int): Int {
val dq = ArrayDeque<Pair<Int, Int>>()
var ans = Int.MIN_VALUE
for ((x, y) in points) {
while (dq.isNotEmpty() && x - dq.first().second > k) dq.removeFirst()
if (dq.isNotEmpty()) ans = maxOf(ans, y + x + dq.first().first)
while (dq.isNotEmpty() && dq.last().first <= y - x) dq.removeLast()
dq.addLast(y - x to x)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
deffindMaxValueOfEquation(self, points: list[list[int]], k: int) -> int:
from collections import deque
dq = deque()
ans = float('-inf')
for x, y in points:
while dq and x - dq[0][1] > k:
dq.popleft()
if dq:
ans = max(ans, y + x + dq[0][0])
while dq and dq[-1][0] <= y - x:
dq.pop()
dq.append((y - x, x))
return ans
impl Solution {
pubfnfind_max_value_of_equation(points: Vec<Vec<i32>>, k: i32) -> i32 {
use std::collections::VecDeque;
letmut dq = VecDeque::new();
letmut ans =i32::MIN;
for pt in points {
let x = pt[0];
let y = pt[1];
whilelet Some(&(_, xi)) = dq.front() {
if x - xi > k {
dq.pop_front();
} else {
break;
}
}
iflet Some(&(v, _)) = dq.front() {
ans = ans.max(y + x + v);
}
whilelet Some(&(v, _)) = dq.back() {
if v <= y - x {
dq.pop_back();
} else {
break;
}
}
dq.push_back((y - x, x));
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
findMaxValueOfEquation(points: number[][], k: number):number {
constdq: [number, number][] = [];
letans=-Infinity;
for (const [x, y] ofpoints) {
while (dq.length&&x-dq[0][1] >k) dq.shift();
if (dq.length) ans= Math.max(ans, y+x+dq[0][0]);
while (dq.length&&dq[dq.length-1][0] <=y-x) dq.pop();
dq.push([y-x, x]);
}
returnans;
}
}