Skip to content

Commit edf768d

Browse files
committed
Add solution #1499
1 parent d52af71 commit edf768d

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,331 LeetCode solutions in JavaScript
1+
# 1,332 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1146,6 +1146,7 @@
11461146
1496|[Path Crossing](./solutions/1496-path-crossing.js)|Easy|
11471147
1497|[Check If Array Pairs Are Divisible by k](./solutions/1497-check-if-array-pairs-are-divisible-by-k.js)|Medium|
11481148
1498|[Number of Subsequences That Satisfy the Given Sum Condition](./solutions/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.js)|Medium|
1149+
1499|[Max Value of Equation](./solutions/1499-max-value-of-equation.js)|Hard|
11491150
1502|[Can Make Arithmetic Progression From Sequence](./solutions/1502-can-make-arithmetic-progression-from-sequence.js)|Easy|
11501151
1507|[Reformat Date](./solutions/1507-reformat-date.js)|Easy|
11511152
1512|[Number of Good Pairs](./solutions/1512-number-of-good-pairs.js)|Easy|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1499. Max Value of Equation
3+
* https://leetcode.com/problems/max-value-of-equation/
4+
* Difficulty: Hard
5+
*
6+
* You are given an array points containing the coordinates of points on a 2D plane,
7+
* sorted by the x-values, where points[i] = [xi, yi] such that xi < xj for all
8+
* 1 <= i < j <= points.length. You are also given an integer k.
9+
*
10+
* Return the maximum value of the equation yi + yj + |xi - xj| where |xi - xj| <= k
11+
* and 1 <= i < j <= points.length.
12+
*
13+
* It is guaranteed that there exists at least one pair of points that satisfy the
14+
* constraint |xi - xj| <= k.
15+
*/
16+
17+
/**
18+
* @param {number[][]} points
19+
* @param {number} k
20+
* @return {number}
21+
*/
22+
var findMaxValueOfEquation = function(points, k) {
23+
let result = -Infinity;
24+
const q = [];
25+
26+
for (let j = 0; j < points.length; j++) {
27+
const [xj, yj] = points[j];
28+
29+
while (q.length && xj - points[q[0]][0] > k) {
30+
q.shift();
31+
}
32+
33+
if (q.length) {
34+
const [xi, yi] = points[q[0]];
35+
result = Math.max(result, yi + yj + xj - xi);
36+
}
37+
38+
while (q.length && yj - xj >= points[q[q.length - 1]][1] - points[q[q.length - 1]][0]) {
39+
q.pop();
40+
}
41+
42+
q.push(j);
43+
}
44+
45+
return result;
46+
};

0 commit comments

Comments
 (0)