LC.P1499[满足不等式的最大值]

方法一:双端队列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int findMaxValueOfEquation(int[][] points, int k) {
int ans = Integer.MIN_VALUE;
// (xi, yi - xi)
Deque<int[]> q = new ArrayDeque<>();
for (int[] point : points) {
int x = point[0], y = point[1];
while (!q.isEmpty() && q.peekFirst()[0] < x - k) q.pollFirst();
if (!q.isEmpty()) {
ans = Math.max(ans, x + y + q.peekFirst()[1]);
}
while (!q.isEmpty() && q.peekLast()[1] <= y - x) q.pollLast();
q.offerLast(new int[]{x, y - x});
}
return ans;
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(n)$