Max Value of Equation LeetCode solution in Python

1
class Solution:
2
def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
3
"""
4
Eqn is: yi + yj + |xi - xj|
5
Since points is sorted by x values,
6
therefore, xj will always be greater than xi
7
therefore xi - xj will always be negative
8
So the above eqn can be rewritten as,
9
(yj+xj) + (yi-xi)
10
Now the problem boils down to finding maximum in sliding window of k size.
11
(https://leetcode.com/problems/sliding-window-maximum/discuss/1911533/Python-or-Dequeue-or-Sliding-Window-or-Simple-Solution)
12
"""
13
queue = deque()
14
maxVal = -sys.maxsize
15
for x, y in points:
16
while queue and abs(queue[0][0] - x) > k:
17
queue.popleft()
18

19
if queue:
20
maxVal = max(maxVal, y + x + queue[0][1])
21

22
while queue and queue[-1][1] <= y - x:
23
queue.pop()
24

25
queue.append((x, y - x))
26

27
return maxVal

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0