1499. Max Value of Equation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int findMaxValueOfEquation(int[][] points, int k) {
// 化简原式可得: xj + yj + (yi - xi)

int maxValue = Integer.MIN_VALUE;
Deque<int[]> deque = new LinkedList<>();
for (int[] point : points) {
int x = point[0], y = point[1];
while (!deque.isEmpty() && deque.peek()[0] < x - k) {
deque.pollFirst();
}
if (!deque.isEmpty()) {
maxValue = Math.max(maxValue, x + y + deque.getFirst()[1]);
}
while (!deque.isEmpty() && (y - x) > deque.getLast()[1]) {
deque.removeLast();
}
deque.offerLast(new int[]{x, y - x});
}

return maxValue;
}
}

References

1499. Max Value of Equation