Skip to content

Commit fa76bb9

Browse files
committed
+ problem 1499
1 parent a8341b2 commit fa76bb9

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 1499. Max Value of Equation
2+
You are given an array `points` containing the coordinates of points on a 2D plane, sorted by the x-values, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> such that <code>x<sub>i</sub> < x<sub>j</sub></code> for all `1 <= i < j <= points.length`. You are also given an integer `k`.
3+
4+
Return *the maximum value of the equation* <code>y<sub>i</sub> + y<sub>j</sub> + |x<sub>i</sub> - x<sub>j</sub>|</code> where <code>|x<sub>i</sub> - x<sub>j</sub>| <= k</code> and `1 <= i < j <= points.length`.
5+
6+
It is guaranteed that there exists at least one pair of points that satisfy the constraint <code>|x<sub>i</sub> - x<sub>j</sub>| <= k</code>.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> points = [[1,3],[2,0],[5,10],[6,-10]], k = 1
11+
<strong>Output:</strong> 4
12+
<strong>Explanation:</strong> The first two points satisfy the condition |xi - xj| <= 1 and if we calculate the equation we get 3 + 0 + |1 - 2| = 4. Third and fourth points also satisfy the condition and give a value of 10 + -10 + |5 - 6| = 1.
13+
No other pairs satisfy the condition, so we return the max of 4 and 1.
14+
</pre>
15+
16+
#### Example 2:
17+
<pre>
18+
<strong>Input:</strong> points = [[0,0],[3,0],[9,2]], k = 3
19+
<strong>Output:</strong> 3
20+
<strong>Explanation:</strong> Only the first two points have an absolute difference of 3 or less in the x-values, and give the value of 0 + 0 + |0 - 3| = 3.
21+
</pre>
22+
23+
#### Constraints:
24+
* <code>2 <= points.length <= 10<sup>5</sup></code>
25+
* `points[i].length == 2`
26+
* <code>-10<sup>8</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>8</sup></code>
27+
* <code>0 <= k <= 2 * 10<sup>8</sup></code>
28+
* <code>x<sub>i</sub> < x<sub>j</sub></code> for all <code>1 <= i < j <= points.length</code>
29+
* <code>x<sub>i</sub></code> form a strictly increasing sequence.
30+
31+
## Solutions (Rust)
32+
33+
### 1. Solution
34+
```Rust
35+
use std::collections::VecDeque;
36+
37+
impl Solution {
38+
pub fn find_max_value_of_equation(points: Vec<Vec<i32>>, k: i32) -> i32 {
39+
let mut deque = VecDeque::new();
40+
let mut ret = i32::MIN;
41+
42+
for j in 0..points.len() {
43+
let (xj, yj) = (points[j][0], points[j][1]);
44+
45+
while xj - deque.front().unwrap_or(&(xj, 0)).0 > k {
46+
deque.pop_front();
47+
}
48+
49+
if let Some(&(xi, yi)) = deque.front() {
50+
ret = ret.max(yi + yj + xj - xi);
51+
}
52+
53+
while let Some(&(xi, yi)) = deque.back() {
54+
if yi - xi <= yj - xj {
55+
deque.pop_back();
56+
} else {
57+
break;
58+
}
59+
}
60+
61+
deque.push_back((xj, yj));
62+
}
63+
64+
ret
65+
}
66+
}
67+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 1499. 满足不等式的最大值
2+
给你一个数组 `points` 和一个整数 `k` 。数组中每个元素都表示二维平面上的点的坐标,并按照横坐标 x 的值从小到大排序。也就是说 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> ,并且在 `1 <= i < j <= points.length` 的前提下, <code>x<sub>i</sub> < x<sub>j</sub></code> 总成立。
3+
4+
请你找出 <code>y<sub>i</sub> + y<sub>j</sub> + |x<sub>i</sub> - x<sub>j</sub>|</code> 的 **最大值**,其中 <code>|x<sub>i</sub> - x<sub>j</sub>| <= k</code> 且 `1 <= i < j <= points.length`
5+
6+
题目测试数据保证至少存在一对能够满足 <code>|x<sub>i</sub> - x<sub>j</sub>| <= k</code> 的点。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> points = [[1,3],[2,0],[5,10],[6,-10]], k = 1
11+
<strong>输出:</strong> 4
12+
<strong>解释:</strong> 前两个点满足 |xi - xj| <= 1 ,代入方程计算,则得到值 3 + 0 + |1 - 2| = 4 。第三个和第四个点也满足条件,得到值 10 + -10 + |5 - 6| = 1 。
13+
没有其他满足条件的点,所以返回 4 和 1 中最大的那个。
14+
</pre>
15+
16+
#### 示例 2:
17+
<pre>
18+
<strong>输入:</strong> points = [[0,0],[3,0],[9,2]], k = 3
19+
<strong>输出:</strong> 3
20+
<strong>解释:</strong> 只有前两个点满足 |xi - xj| <= 3 ,代入方程后得到值 0 + 0 + |0 - 3| = 3 。
21+
</pre>
22+
23+
#### 提示:
24+
* <code>2 <= points.length <= 10<sup>5</sup></code>
25+
* `points[i].length == 2`
26+
* <code>-10<sup>8</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>8</sup></code>
27+
* <code>0 <= k <= 2 * 10<sup>8</sup></code>
28+
* 对于所有的`1 <= i < j <= points.length``points[i][0] < points[j][0]` 都成立。也就是说,<code>x<sub>i</sub></code> 是严格递增的。
29+
30+
## 题解 (Rust)
31+
32+
### 1. 题解
33+
```Rust
34+
use std::collections::VecDeque;
35+
36+
impl Solution {
37+
pub fn find_max_value_of_equation(points: Vec<Vec<i32>>, k: i32) -> i32 {
38+
let mut deque = VecDeque::new();
39+
let mut ret = i32::MIN;
40+
41+
for j in 0..points.len() {
42+
let (xj, yj) = (points[j][0], points[j][1]);
43+
44+
while xj - deque.front().unwrap_or(&(xj, 0)).0 > k {
45+
deque.pop_front();
46+
}
47+
48+
if let Some(&(xi, yi)) = deque.front() {
49+
ret = ret.max(yi + yj + xj - xi);
50+
}
51+
52+
while let Some(&(xi, yi)) = deque.back() {
53+
if yi - xi <= yj - xj {
54+
deque.pop_back();
55+
} else {
56+
break;
57+
}
58+
}
59+
60+
deque.push_back((xj, yj));
61+
}
62+
63+
ret
64+
}
65+
}
66+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::collections::VecDeque;
2+
3+
impl Solution {
4+
pub fn find_max_value_of_equation(points: Vec<Vec<i32>>, k: i32) -> i32 {
5+
let mut deque = VecDeque::new();
6+
let mut ret = i32::MIN;
7+
8+
for j in 0..points.len() {
9+
let (xj, yj) = (points[j][0], points[j][1]);
10+
11+
while xj - deque.front().unwrap_or(&(xj, 0)).0 > k {
12+
deque.pop_front();
13+
}
14+
15+
if let Some(&(xi, yi)) = deque.front() {
16+
ret = ret.max(yi + yj + xj - xi);
17+
}
18+
19+
while let Some(&(xi, yi)) = deque.back() {
20+
if yi - xi <= yj - xj {
21+
deque.pop_back();
22+
} else {
23+
break;
24+
}
25+
}
26+
27+
deque.push_back((xj, yj));
28+
}
29+
30+
ret
31+
}
32+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,7 @@
876876
[1493][1493l]|[Longest Subarray of 1's After Deleting One Element][1493] |![rb]&nbsp;&nbsp;![rs]
877877
[1496][1496l]|[Path Crossing][1496] |![rs]
878878
[1497][1497l]|[Check If Array Pairs Are Divisible by k][1497] |![rs]
879+
[1499][1499l]|[Max Value of Equation][1499] |![rs]
879880
[1502][1502l]|[Can Make Arithmetic Progression From Sequence][1502] |![rb]&nbsp;&nbsp;![rs]
880881
[1503][1503l]|[Last Moment Before All Ants Fall Out of a Plank][1503] |![rb]&nbsp;&nbsp;![rs]
881882
[1504][1504l]|[Count Submatrices With All Ones][1504] |![rs]
@@ -2273,6 +2274,7 @@
22732274
[1493]:Problemset/1493-Longest%20Subarray%20of%201's%20After%20Deleting%20One%20Element/README.md#1493-longest-subarray-of-1s-after-deleting-one-element
22742275
[1496]:Problemset/1496-Path%20Crossing/README.md#1496-path-crossing
22752276
[1497]:Problemset/1497-Check%20If%20Array%20Pairs%20Are%20Divisible%20by%20k/README.md#1497-check-if-array-pairs-are-divisible-by-k
2277+
[1499]:Problemset/1499-Max%20Value%20of%20Equation/README.md#1499-max-value-of-equation
22762278
[1502]:Problemset/1502-Can%20Make%20Arithmetic%20Progression%20From%20Sequence/README.md#1502-can-make-arithmetic-progression-from-sequence
22772279
[1503]:Problemset/1503-Last%20Moment%20Before%20All%20Ants%20Fall%20Out%20of%20a%20Plank/README.md#1503-last-moment-before-all-ants-fall-out-of-a-plank
22782280
[1504]:Problemset/1504-Count%20Submatrices%20With%20All%20Ones/README.md#1504-count-submatrices-with-all-ones
@@ -3669,6 +3671,7 @@
36693671
[1493l]:https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/
36703672
[1496l]:https://leetcode.com/problems/path-crossing/
36713673
[1497l]:https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/
3674+
[1499l]:https://leetcode.com/problems/max-value-of-equation/
36723675
[1502l]:https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/
36733676
[1503l]:https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank/
36743677
[1504l]:https://leetcode.com/problems/count-submatrices-with-all-ones/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,7 @@
876876
[1493][1493l]|[删掉一个元素以后全为 1 的最长子数组][1493] |![rb]&nbsp;&nbsp;![rs]
877877
[1496][1496l]|[判断路径是否相交][1496] |![rs]
878878
[1497][1497l]|[检查数组对是否可以被 k 整除][1497] |![rs]
879+
[1499][1499l]|[满足不等式的最大值][1499] |![rs]
879880
[1502][1502l]|[判断能否形成等差数列][1502] |![rb]&nbsp;&nbsp;![rs]
880881
[1503][1503l]|[所有蚂蚁掉下来前的最后一刻][1503] |![rb]&nbsp;&nbsp;![rs]
881882
[1504][1504l]|[统计全 1 子矩形][1504] |![rs]
@@ -2273,6 +2274,7 @@
22732274
[1493]:Problemset/1493-Longest%20Subarray%20of%201's%20After%20Deleting%20One%20Element/README_CN.md#1493-删掉一个元素以后全为-1-的最长子数组
22742275
[1496]:Problemset/1496-Path%20Crossing/README_CN.md#1496-判断路径是否相交
22752276
[1497]:Problemset/1497-Check%20If%20Array%20Pairs%20Are%20Divisible%20by%20k/README_CN.md#1497-检查数组对是否可以被-k-整除
2277+
[1499]:Problemset/1499-Max%20Value%20of%20Equation/README_CN.md#1499-满足不等式的最大值
22762278
[1502]:Problemset/1502-Can%20Make%20Arithmetic%20Progression%20From%20Sequence/README_CN.md#1502-判断能否形成等差数列
22772279
[1503]:Problemset/1503-Last%20Moment%20Before%20All%20Ants%20Fall%20Out%20of%20a%20Plank/README_CN.md#1503-所有蚂蚁掉下来前的最后一刻
22782280
[1504]:Problemset/1504-Count%20Submatrices%20With%20All%20Ones/README_CN.md#1504-统计全-1-子矩形
@@ -3669,6 +3671,7 @@
36693671
[1493l]:https://leetcode.cn/problems/longest-subarray-of-1s-after-deleting-one-element/
36703672
[1496l]:https://leetcode.cn/problems/path-crossing/
36713673
[1497l]:https://leetcode.cn/problems/check-if-array-pairs-are-divisible-by-k/
3674+
[1499l]:https://leetcode.cn/problems/max-value-of-equation/
36723675
[1502l]:https://leetcode.cn/problems/can-make-arithmetic-progression-from-sequence/
36733676
[1503l]:https://leetcode.cn/problems/last-moment-before-all-ants-fall-out-of-a-plank/
36743677
[1504l]:https://leetcode.cn/problems/count-submatrices-with-all-ones/

0 commit comments

Comments
 (0)