#1499

Max Value of Equation

candidate master · 1560 · lc hard +32 · verified · 45% accepted · 1,412 likes · top 28%

Description

You have a list of 2D points points[i] = [xi, yi] sorted by strictly increasing x-values, and an integer k. Among all pairs (i, j) with i < j and |xi - xj| <= k, return the maximum value of yi + yj + |xi - xj|. At least one valid pair is guaranteed to exist.

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.

Code

1
2
3