-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1499-max-value-of-equation.js
46 lines (39 loc) · 1.16 KB
/
1499-max-value-of-equation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 1499. Max Value of Equation
* https://leetcode.com/problems/max-value-of-equation/
* Difficulty: Hard
*
* 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 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.
*/
/**
* @param {number[][]} points
* @param {number} k
* @return {number}
*/
var findMaxValueOfEquation = function(points, k) {
let result = -Infinity;
const q = [];
for (let j = 0; j < points.length; j++) {
const [xj, yj] = points[j];
while (q.length && xj - points[q[0]][0] > k) {
q.shift();
}
if (q.length) {
const [xi, yi] = points[q[0]];
result = Math.max(result, yi + yj + xj - xi);
}
while (q.length && yj - xj >= points[q[q.length - 1]][1] - points[q[q.length - 1]][0]) {
q.pop();
}
q.push(j);
}
return result;
};