mirror of https://github.com/doocs/leetcode.git
feat: add solutions to lc problem: No.1499
No.1499.Max Value of Equation
This commit is contained in:
parent
91721d617b
commit
e010649f30
|
|
@ -57,6 +57,7 @@
|
|||
- [下一个更大元素 I](/solution/0400-0499/0496.Next%20Greater%20Element%20I/README.md) - `单调栈`
|
||||
- [每日温度](/solution/0700-0799/0739.Daily%20Temperatures/README.md) - `单调栈`
|
||||
- [滑动窗口最大值](/solution/0200-0299/0239.Sliding%20Window%20Maximum/README.md) - `单调队列`
|
||||
- [满足不等式的最大值](/solution/1400-1499/1499.Max%20Value%20of%20Equation/README.md) - `单调队列`
|
||||
|
||||
### 3. 搜索
|
||||
|
||||
|
|
|
|||
|
|
@ -55,6 +55,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
|
|||
- [Next Greater Element I](/solution/0400-0499/0496.Next%20Greater%20Element%20I/README_EN.md) - `Monotonic Stack`
|
||||
- [Daily Temperatures](/solution/0700-0799/0739.Daily%20Temperatures/README_EN.md) - `Monotonic Stack`
|
||||
- [Sliding Window Maximum](/solution/0200-0299/0239.Sliding%20Window%20Maximum/README_EN.md) - `Monotonic Queue`
|
||||
- [Max Value of Equation](/solution/1400-1499/1499.Max%20Value%20of%20Equation/README_EN.md) - `Monotonic Queue`
|
||||
|
||||
### 3. Search
|
||||
|
||||
|
|
|
|||
|
|
@ -44,6 +44,12 @@
|
|||
|
||||
<!-- 这里可写通用的实现逻辑 -->
|
||||
|
||||
**方法一:单调队列**
|
||||
|
||||
区间(窗口)最值问题,使用单调队列优化。q 按 `y - x` 单调递减。
|
||||
|
||||
时间复杂度 O(n)。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
|
@ -51,7 +57,19 @@
|
|||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```python
|
||||
|
||||
class Solution:
|
||||
def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
|
||||
q = deque([points[0]])
|
||||
ans = float('-inf')
|
||||
for x, y in points[1:]:
|
||||
while q and x - q[0][0] > k:
|
||||
q.popleft()
|
||||
if q:
|
||||
ans = max(ans, x + y + q[0][1] - q[0][0])
|
||||
while q and y - x > q[-1][1] - q[-1][0]:
|
||||
q.pop()
|
||||
q.append([x, y])
|
||||
return ans
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
|
@ -59,7 +77,77 @@
|
|||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int findMaxValueOfEquation(int[][] points, int k) {
|
||||
Deque<int[]> q = new ArrayDeque<>();
|
||||
int ans = Integer.MIN_VALUE;
|
||||
for (int[] p : points) {
|
||||
int x = p[0], y = p[1];
|
||||
while (!q.isEmpty() && x - q.peekFirst()[0] > k) {
|
||||
q.poll();
|
||||
}
|
||||
if (!q.isEmpty()) {
|
||||
ans = Math.max(ans, y + x + q.peekFirst()[1] - q.peekFirst()[0]);
|
||||
}
|
||||
while (!q.isEmpty() && y - x > q.peekLast()[1] - q.peekLast()[0]) {
|
||||
q.pollLast();
|
||||
}
|
||||
q.offer(p);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
int findMaxValueOfEquation(vector<vector<int>>& points, int k) {
|
||||
deque<vector<int>> q;
|
||||
int ans = INT_MIN;
|
||||
for (auto& p : points)
|
||||
{
|
||||
int x = p[0], y = p[1];
|
||||
while (!q.empty() && x - q.front()[0] > k) q.pop_front();
|
||||
if (!q.empty()) ans = max(ans, y + x + q.front()[1] - q.front()[0]);
|
||||
while (!q.empty() && y - x > q.back()[1] - q.back()[0]) q.pop_back();
|
||||
q.push_back(p);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
func findMaxValueOfEquation(points [][]int, k int) int {
|
||||
q := [][]int{}
|
||||
ans := math.MinInt32
|
||||
for _, p := range points {
|
||||
x, y := p[0], p[1]
|
||||
for len(q) > 0 && x-q[0][0] > k {
|
||||
q = q[1:]
|
||||
}
|
||||
if len(q) > 0 {
|
||||
ans = max(ans, y+x+q[0][1]-q[0][0])
|
||||
}
|
||||
for len(q) > 0 && y-x > q[len(q)-1][1]-q[len(q)-1][0] {
|
||||
q = q[:len(q)-1]
|
||||
}
|
||||
q = append(q, p)
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
|
|
|||
|
|
@ -47,13 +47,95 @@ No other pairs satisfy the condition, so we return the max of 4 and 1.
|
|||
### **Python3**
|
||||
|
||||
```python
|
||||
|
||||
class Solution:
|
||||
def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
|
||||
q = deque([points[0]])
|
||||
ans = float('-inf')
|
||||
for x, y in points[1:]:
|
||||
while q and x - q[0][0] > k:
|
||||
q.popleft()
|
||||
if q:
|
||||
ans = max(ans, x + y + q[0][1] - q[0][0])
|
||||
while q and y - x > q[-1][1] - q[-1][0]:
|
||||
q.pop()
|
||||
q.append([x, y])
|
||||
return ans
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int findMaxValueOfEquation(int[][] points, int k) {
|
||||
Deque<int[]> q = new ArrayDeque<>();
|
||||
int ans = Integer.MIN_VALUE;
|
||||
for (int[] p : points) {
|
||||
int x = p[0], y = p[1];
|
||||
while (!q.isEmpty() && x - q.peekFirst()[0] > k) {
|
||||
q.poll();
|
||||
}
|
||||
if (!q.isEmpty()) {
|
||||
ans = Math.max(ans, y + x + q.peekFirst()[1] - q.peekFirst()[0]);
|
||||
}
|
||||
while (!q.isEmpty() && y - x > q.peekLast()[1] - q.peekLast()[0]) {
|
||||
q.pollLast();
|
||||
}
|
||||
q.offer(p);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
int findMaxValueOfEquation(vector<vector<int>>& points, int k) {
|
||||
deque<vector<int>> q;
|
||||
int ans = INT_MIN;
|
||||
for (auto& p : points)
|
||||
{
|
||||
int x = p[0], y = p[1];
|
||||
while (!q.empty() && x - q.front()[0] > k) q.pop_front();
|
||||
if (!q.empty()) ans = max(ans, y + x + q.front()[1] - q.front()[0]);
|
||||
while (!q.empty() && y - x > q.back()[1] - q.back()[0]) q.pop_back();
|
||||
q.push_back(p);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
func findMaxValueOfEquation(points [][]int, k int) int {
|
||||
q := [][]int{}
|
||||
ans := math.MinInt32
|
||||
for _, p := range points {
|
||||
x, y := p[0], p[1]
|
||||
for len(q) > 0 && x-q[0][0] > k {
|
||||
q = q[1:]
|
||||
}
|
||||
if len(q) > 0 {
|
||||
ans = max(ans, y+x+q[0][1]-q[0][0])
|
||||
}
|
||||
for len(q) > 0 && y-x > q[len(q)-1][1]-q[len(q)-1][0] {
|
||||
q = q[:len(q)-1]
|
||||
}
|
||||
q = append(q, p)
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
class Solution {
|
||||
public:
|
||||
int findMaxValueOfEquation(vector<vector<int>>& points, int k) {
|
||||
deque<vector<int>> q;
|
||||
int ans = INT_MIN;
|
||||
for (auto& p : points)
|
||||
{
|
||||
int x = p[0], y = p[1];
|
||||
while (!q.empty() && x - q.front()[0] > k) q.pop_front();
|
||||
if (!q.empty()) ans = max(ans, y + x + q.front()[1] - q.front()[0]);
|
||||
while (!q.empty() && y - x > q.back()[1] - q.back()[0]) q.pop_back();
|
||||
q.push_back(p);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
func findMaxValueOfEquation(points [][]int, k int) int {
|
||||
q := [][]int{}
|
||||
ans := math.MinInt32
|
||||
for _, p := range points {
|
||||
x, y := p[0], p[1]
|
||||
for len(q) > 0 && x-q[0][0] > k {
|
||||
q = q[1:]
|
||||
}
|
||||
if len(q) > 0 {
|
||||
ans = max(ans, y+x+q[0][1]-q[0][0])
|
||||
}
|
||||
for len(q) > 0 && y-x > q[len(q)-1][1]-q[len(q)-1][0] {
|
||||
q = q[:len(q)-1]
|
||||
}
|
||||
q = append(q, p)
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
class Solution {
|
||||
public int findMaxValueOfEquation(int[][] points, int k) {
|
||||
Deque<int[]> q = new ArrayDeque<>();
|
||||
int ans = Integer.MIN_VALUE;
|
||||
for (int[] p : points) {
|
||||
int x = p[0], y = p[1];
|
||||
while (!q.isEmpty() && x - q.peekFirst()[0] > k) {
|
||||
q.poll();
|
||||
}
|
||||
if (!q.isEmpty()) {
|
||||
ans = Math.max(ans, y + x + q.peekFirst()[1] - q.peekFirst()[0]);
|
||||
}
|
||||
while (!q.isEmpty() && y - x > q.peekLast()[1] - q.peekLast()[0]) {
|
||||
q.pollLast();
|
||||
}
|
||||
q.offer(p);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
class Solution:
|
||||
def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
|
||||
q = deque([points[0]])
|
||||
ans = float('-inf')
|
||||
for x, y in points[1:]:
|
||||
while q and x - q[0][0] > k:
|
||||
q.popleft()
|
||||
if q:
|
||||
ans = max(ans, x + y + q[0][1] - q[0][0])
|
||||
while q and y - x > q[-1][1] - q[-1][0]:
|
||||
q.pop()
|
||||
q.append([x, y])
|
||||
return ans
|
||||
|
|
@ -26,7 +26,6 @@ class Node:
|
|||
|
||||
|
||||
class CountIntervals:
|
||||
|
||||
def __init__(self):
|
||||
self.tree = Node()
|
||||
|
||||
|
|
@ -36,6 +35,7 @@ class CountIntervals:
|
|||
def count(self) -> int:
|
||||
return self.tree.tot
|
||||
|
||||
|
||||
# Your CountIntervals object will be instantiated and called as such:
|
||||
# obj = CountIntervals()
|
||||
# obj.add(left,right)
|
||||
|
|
|
|||
Loading…
Reference in New Issue