当前位置:网站首页>1499. The maximum pile.then/deque
1499. The maximum pile.then/deque
2022-08-10 08:29:00 【Yu Niangniang】
1499. The maximum value that satisfies the inequality
You are given an array
points
and an integerk
.Each element in the array represents the coordinates of a point on a two-dimensional plane, and is sorted according to the value of the abscissa x from small to large.That is,points[i] = [xi, yi]
, and under the premise of1 <= i < j <= points.length
,xi
is always established. Please find the maximum value of
yi + yj + |xi - xj|
, where|xi - xj|<= k
and1 <= i < j <= points.length
.The item test data guarantees that there are at least one pair of points that satisfy
|xi - xj| <= k
.Example 1:
Input:points = [[1,3],[2,0],[5,10],[6,-10]], k = 1Output:4Explanation: The first two points satisfy |xi - xj| <= 1 , substituting into the equation for calculation, the value 3 + 0 + |1 - 2| = 4 is obtained.The third and fourth points also satisfy the condition, resulting in the value 10 + -10 + |5 - 6| = 1 .There are no other points that satisfy the condition, so the largest of 4 and 1 is returned.Example 2:
Input:points = [[0,0],[3,0],[9,2]], k = 3Output:3Explanation: Only the first two points satisfy |xi - xj| <= 3 , and after substituting into the equation, the value 0 + 0 + |0 - 3| = 3 is obtained.Tip:
2 <= points.length <= 10^5
points[i].length == 2
-10^8 <= points[i][0], points[i][1] <= 10^8
0 <= k <= 2 * 10^8
- For all
1 <= i < j <= points.length
,points[i][0] < points[j][0]
holds.That is,xi
is strictly increasing.Source: LeetCode
Link: https://leetcode.cn/problems/max-value-of-equation
The copyright belongs to LeetCode.For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.
Results of the questions
Success, heap, later reference tag monotonic queue
Method 1: Heap
yi + yj + |xi - xj|, because xi The main feature is that it adopts the lazy deletion strategy, saves the position of the previous index and yi-xi, and sorts the heap according to yi-xi from large to small 1. Delete the distance between the top of the heap and x (lazy deletion) 2. Non-empty comparison max(yi+yj+heap top, ans) 3. The current element {xi,yi-xi} is added to the heap Time complexity: O(nlogn) Space complexity: O(n) 1. Consider two situations: - the new value is smaller than the old value, may the new value be counted as a valid value in the answer?possible.The previous old value is dequeued, and the new value may become the maximum - the new value is greater than the old value, can the old value be included as a valid value in the answer?impossible.The old value is dequeued before, and the new value has not been dequeued, so the old value is invalid. 2. To sum up, this question can maintain a monotonic decline.The maximum value is first, and the minimum value is last.The previous value is dequeued if the distance is exceeded, and the latter value is smaller than the new value, dequeued, and then the new value is added to the end of the queue.In this way, the complexity of O(n) is used to maintain the same maximum value as the heap. Time complexity: O(n) Space complexity: O(n) class Solution {public int findMaxValueOfEquation(int[][] points, int k) {PriorityQueue
Method 2: Deque (Monotone Queue)
class Solution {public int findMaxValueOfEquation(int[][] points, int k) {int n = points.length;int[] deque = new int[n];int start = 0;int end = 0;int ans = Integer.MIN_VALUE;for (int i = 0; i < n; i++) {int[] point = points[i];while (end!=start && point[0] - points[deque[start]][0] > k) ++start;if (end!=start) ans = Math.max(ans, point[0] + point[1] + (points[deque[start]][1]-points[deque[start]][0]));int v = point[1] - point[0];while (end!=start && points[deque[end-1]][1]-points[deque[end-1]][0] < v)--end;deque[end++]=i;}return ans;}}
边栏推荐
- Relaxation class: the boss will martial arts, who also can not hold up against!The charm of six sigma training
- In the SQL SERVER database, if the data of the table is added, deleted, or modified, will the index of the table be recorded in the ldf log?
- ARM结构体系3:ARM指令的寻址和异常中断处理
- Uni-app develops WeChat applet using local images as background images
- DGIOT supports industrial equipment rental and remote control
- Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
- Pieces of TensorFlow 2.9 (1)
- qrcode-----生成二维码
- Uni-app开发微信小程序使用本地图片做背景图
- StringUtils的具体操作
猜你喜欢
Introduction to C integer data storage
DGIOT 30 million meters set pressure reading
Docker搭建Mysql一主一从
J9数字论:Web3.0+互联网电商会引起怎样的火花?
Delphi实现的一个文件在线查询显示下载功能
Rust学习:6.3_复合类型之元组
Using the color picker
Relaxation class: the boss will martial arts, who also can not hold up against!The charm of six sigma training
解决win10win7win8系统找不到指定的模块,注册不了大漠插件的问题
Rust learning: 6.3_ Tuples of composite types
随机推荐
day16--抓包工具Charles的使用
VMware ESX Server常用命令行
PTA 习题2.2 数组循环左移
30条实用MySQL优化法则
时序动作定位 | ACGNet:弱监督时序动作定位的动作补充图网络(AAAI 2022)
机器人控制器编程实践指导书旧版-实践二 传感器(模拟量)
【OAuth2】十九、OpenID Connect 动态客户端注册
UGUI—事件,iTween插件
Pieces of TensorFlow 2.9 (1)
推荐几个高质量的软件测试实战项目
js--------对象数组转换成二维数组(excel表格导出)
winget包管理器
硬件工程师90天学习资料及笔记汇总
dayjs-----时间格式化
卷积神经网络卷积层公式,卷积神经网络运算公式
TensorFlow 2.9的零零碎碎(一)
js-----数组转换成树形结构
js reduce
同步锁synchronized追本溯源
Rust学习:6.5_复合类型之数组