Skip to content

Commit e010649

Browse files
committed
feat: add solutions to lc problem: No.1499
No.1499.Max Value of Equation
1 parent 91721d6 commit e010649

File tree

9 files changed

+249
-3
lines changed

9 files changed

+249
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
- [下一个更大元素 I](/solution/0400-0499/0496.Next%20Greater%20Element%20I/README.md) - `单调栈`
5858
- [每日温度](/solution/0700-0799/0739.Daily%20Temperatures/README.md) - `单调栈`
5959
- [滑动窗口最大值](/solution/0200-0299/0239.Sliding%20Window%20Maximum/README.md) - `单调队列`
60+
- [满足不等式的最大值](/solution/1400-1499/1499.Max%20Value%20of%20Equation/README.md) - `单调队列`
6061

6162
### 3. 搜索
6263

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
5555
- [Next Greater Element I](/solution/0400-0499/0496.Next%20Greater%20Element%20I/README_EN.md) - `Monotonic Stack`
5656
- [Daily Temperatures](/solution/0700-0799/0739.Daily%20Temperatures/README_EN.md) - `Monotonic Stack`
5757
- [Sliding Window Maximum](/solution/0200-0299/0239.Sliding%20Window%20Maximum/README_EN.md) - `Monotonic Queue`
58+
- [Max Value of Equation](/solution/1400-1499/1499.Max%20Value%20of%20Equation/README_EN.md) - `Monotonic Queue`
5859

5960
### 3. Search
6061

solution/1400-1499/1499.Max Value of Equation/README.md

+89-1
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,110 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:单调队列**
48+
49+
区间(窗口)最值问题,使用单调队列优化。q 按 `y - x` 单调递减。
50+
51+
时间复杂度 O(n)。
52+
4753
<!-- tabs:start -->
4854

4955
### **Python3**
5056

5157
<!-- 这里可写当前语言的特殊实现逻辑 -->
5258

5359
```python
54-
60+
class Solution:
61+
def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
62+
q = deque([points[0]])
63+
ans = float('-inf')
64+
for x, y in points[1:]:
65+
while q and x - q[0][0] > k:
66+
q.popleft()
67+
if q:
68+
ans = max(ans, x + y + q[0][1] - q[0][0])
69+
while q and y - x > q[-1][1] - q[-1][0]:
70+
q.pop()
71+
q.append([x, y])
72+
return ans
5573
```
5674

5775
### **Java**
5876

5977
<!-- 这里可写当前语言的特殊实现逻辑 -->
6078

6179
```java
80+
class Solution {
81+
public int findMaxValueOfEquation(int[][] points, int k) {
82+
Deque<int[]> q = new ArrayDeque<>();
83+
int ans = Integer.MIN_VALUE;
84+
for (int[] p : points) {
85+
int x = p[0], y = p[1];
86+
while (!q.isEmpty() && x - q.peekFirst()[0] > k) {
87+
q.poll();
88+
}
89+
if (!q.isEmpty()) {
90+
ans = Math.max(ans, y + x + q.peekFirst()[1] - q.peekFirst()[0]);
91+
}
92+
while (!q.isEmpty() && y - x > q.peekLast()[1] - q.peekLast()[0]) {
93+
q.pollLast();
94+
}
95+
q.offer(p);
96+
}
97+
return ans;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int findMaxValueOfEquation(vector<vector<int>>& points, int k) {
108+
deque<vector<int>> q;
109+
int ans = INT_MIN;
110+
for (auto& p : points)
111+
{
112+
int x = p[0], y = p[1];
113+
while (!q.empty() && x - q.front()[0] > k) q.pop_front();
114+
if (!q.empty()) ans = max(ans, y + x + q.front()[1] - q.front()[0]);
115+
while (!q.empty() && y - x > q.back()[1] - q.back()[0]) q.pop_back();
116+
q.push_back(p);
117+
}
118+
return ans;
119+
}
120+
};
121+
```
62122
123+
### **Go**
124+
125+
```go
126+
func findMaxValueOfEquation(points [][]int, k int) int {
127+
q := [][]int{}
128+
ans := math.MinInt32
129+
for _, p := range points {
130+
x, y := p[0], p[1]
131+
for len(q) > 0 && x-q[0][0] > k {
132+
q = q[1:]
133+
}
134+
if len(q) > 0 {
135+
ans = max(ans, y+x+q[0][1]-q[0][0])
136+
}
137+
for len(q) > 0 && y-x > q[len(q)-1][1]-q[len(q)-1][0] {
138+
q = q[:len(q)-1]
139+
}
140+
q = append(q, p)
141+
}
142+
return ans
143+
}
144+
145+
func max(a, b int) int {
146+
if a > b {
147+
return a
148+
}
149+
return b
150+
}
63151
```
64152

65153
### **...**

solution/1400-1499/1499.Max Value of Equation/README_EN.md

+83-1
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,95 @@ No other pairs satisfy the condition, so we return the max of 4 and 1.
4747
### **Python3**
4848

4949
```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
5163
```
5264

5365
### **Java**
5466

5567
```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+
```
56110
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+
}
57139
```
58140

59141
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int findMaxValueOfEquation(vector<vector<int>>& points, int k) {
4+
deque<vector<int>> q;
5+
int ans = INT_MIN;
6+
for (auto& p : points)
7+
{
8+
int x = p[0], y = p[1];
9+
while (!q.empty() && x - q.front()[0] > k) q.pop_front();
10+
if (!q.empty()) ans = max(ans, y + x + q.front()[1] - q.front()[0]);
11+
while (!q.empty() && y - x > q.back()[1] - q.back()[0]) q.pop_back();
12+
q.push_back(p);
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func findMaxValueOfEquation(points [][]int, k int) int {
2+
q := [][]int{}
3+
ans := math.MinInt32
4+
for _, p := range points {
5+
x, y := p[0], p[1]
6+
for len(q) > 0 && x-q[0][0] > k {
7+
q = q[1:]
8+
}
9+
if len(q) > 0 {
10+
ans = max(ans, y+x+q[0][1]-q[0][0])
11+
}
12+
for len(q) > 0 && y-x > q[len(q)-1][1]-q[len(q)-1][0] {
13+
q = q[:len(q)-1]
14+
}
15+
q = append(q, p)
16+
}
17+
return ans
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int findMaxValueOfEquation(int[][] points, int k) {
3+
Deque<int[]> q = new ArrayDeque<>();
4+
int ans = Integer.MIN_VALUE;
5+
for (int[] p : points) {
6+
int x = p[0], y = p[1];
7+
while (!q.isEmpty() && x - q.peekFirst()[0] > k) {
8+
q.poll();
9+
}
10+
if (!q.isEmpty()) {
11+
ans = Math.max(ans, y + x + q.peekFirst()[1] - q.peekFirst()[0]);
12+
}
13+
while (!q.isEmpty() && y - x > q.peekLast()[1] - q.peekLast()[0]) {
14+
q.pollLast();
15+
}
16+
q.offer(p);
17+
}
18+
return ans;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
3+
q = deque([points[0]])
4+
ans = float('-inf')
5+
for x, y in points[1:]:
6+
while q and x - q[0][0] > k:
7+
q.popleft()
8+
if q:
9+
ans = max(ans, x + y + q[0][1] - q[0][0])
10+
while q and y - x > q[-1][1] - q[-1][0]:
11+
q.pop()
12+
q.append([x, y])
13+
return ans

solution/2200-2299/2276.Count Integers in Intervals/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def update(self, l, r, a, b):
2626

2727

2828
class CountIntervals:
29-
3029
def __init__(self):
3130
self.tree = Node()
3231

@@ -36,6 +35,7 @@ def add(self, left: int, right: int) -> None:
3635
def count(self) -> int:
3736
return self.tree.tot
3837

38+
3939
# Your CountIntervals object will be instantiated and called as such:
4040
# obj = CountIntervals()
4141
# obj.add(left,right)

0 commit comments

Comments
 (0)