@@ -47,13 +47,95 @@ No other pairs satisfy the condition, so we return the max of 4 and 1.
47
47
### ** Python3**
48
48
49
49
``` python
50
-
50
+ class Solution :
51
+ def findMaxValueOfEquation (self , points : List[List[int ]], k : int ) -> int :
52
+ q = deque([points[0 ]])
53
+ ans = float (' -inf' )
54
+ for x, y in points[1 :]:
55
+ while q and x - q[0 ][0 ] > k:
56
+ q.popleft()
57
+ if q:
58
+ ans = max (ans, x + y + q[0 ][1 ] - q[0 ][0 ])
59
+ while q and y - x > q[- 1 ][1 ] - q[- 1 ][0 ]:
60
+ q.pop()
61
+ q.append([x, y])
62
+ return ans
51
63
```
52
64
53
65
### ** Java**
54
66
55
67
``` java
68
+ class Solution {
69
+ public int findMaxValueOfEquation (int [][] points , int k ) {
70
+ Deque<int[]> q = new ArrayDeque<> ();
71
+ int ans = Integer . MIN_VALUE ;
72
+ for (int [] p : points) {
73
+ int x = p[0 ], y = p[1 ];
74
+ while (! q. isEmpty() && x - q. peekFirst()[0 ] > k) {
75
+ q. poll();
76
+ }
77
+ if (! q. isEmpty()) {
78
+ ans = Math . max(ans, y + x + q. peekFirst()[1 ] - q. peekFirst()[0 ]);
79
+ }
80
+ while (! q. isEmpty() && y - x > q. peekLast()[1 ] - q. peekLast()[0 ]) {
81
+ q. pollLast();
82
+ }
83
+ q. offer(p);
84
+ }
85
+ return ans;
86
+ }
87
+ }
88
+ ```
89
+
90
+ ### ** C++**
91
+
92
+ ``` cpp
93
+ class Solution {
94
+ public:
95
+ int findMaxValueOfEquation(vector<vector<int >>& points, int k) {
96
+ deque<vector<int >> q;
97
+ int ans = INT_MIN;
98
+ for (auto& p : points)
99
+ {
100
+ int x = p[ 0] , y = p[ 1] ;
101
+ while (!q.empty() && x - q.front()[ 0] > k) q.pop_front();
102
+ if (!q.empty()) ans = max(ans, y + x + q.front()[ 1] - q.front()[ 0] );
103
+ while (!q.empty() && y - x > q.back()[ 1] - q.back()[ 0] ) q.pop_back();
104
+ q.push_back(p);
105
+ }
106
+ return ans;
107
+ }
108
+ };
109
+ ```
56
110
111
+ ### **Go**
112
+
113
+ ```go
114
+ func findMaxValueOfEquation(points [][]int, k int) int {
115
+ q := [][]int{}
116
+ ans := math.MinInt32
117
+ for _, p := range points {
118
+ x, y := p[0], p[1]
119
+ for len(q) > 0 && x-q[0][0] > k {
120
+ q = q[1:]
121
+ }
122
+ if len(q) > 0 {
123
+ ans = max(ans, y+x+q[0][1]-q[0][0])
124
+ }
125
+ for len(q) > 0 && y-x > q[len(q)-1][1]-q[len(q)-1][0] {
126
+ q = q[:len(q)-1]
127
+ }
128
+ q = append(q, p)
129
+ }
130
+ return ans
131
+ }
132
+
133
+ func max(a, b int) int {
134
+ if a > b {
135
+ return a
136
+ }
137
+ return b
138
+ }
57
139
```
58
140
59
141
### ** ...**
0 commit comments