当前位置:网站首页>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
pointsand 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,xiis always established. Please find the maximum value of
yi + yj + |xi - xj|, where|xi - xj|<= kand1 <= 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^5points[i].length == 2-10^8 <= points[i][0], points[i][1] <= 10^80 <= k <= 2 * 10^8- For all
1 <= i < j <= points.length,points[i][0] < points[j][0]holds.That is,xiis 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) {PriorityQueueMethod 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;}}
边栏推荐
- [机缘参悟-65]:《兵者,诡道也》-7-三十六计解读-败战计
- 大体来讲,网站会被攻击分为几种原因
- 大佬们,请问一下,oraclecdc报错没有序列化,可是我看源码中的确是没有继承序列化的,是什么原因
- Rust学习:6.3_复合类型之元组
- 硬件工程师90天学习资料及笔记汇总
- Synchronization lock synchronized traces the source
- DGIOT supports industrial equipment rental and remote control
- 物联网时代下的网络整合推广外包精准化效果-深圳双赢世讯
- Compilation failure:找不到符号
- 【OAuth2】十九、OpenID Connect 动态客户端注册
猜你喜欢

2022-08-01 网工进阶(二十三) VLAN高级技术-VLAN聚合、MUX VLAN
深度剖析“八大排序”(上)_ 探寻一些不为人知的细节

明明加了唯一索引,为什么还是产生重复数据?

VS2013-调试汇编代码-生成asm文件-结构体内存布局-函数参数压栈-调用约定

【OAuth2】十九、OpenID Connect 动态客户端注册

Unity—UGUI控件

Docker搭建Mysql一主一从

Uni-app develops WeChat applet using local images as background images

2022-08-01 网工进阶(二十四) STP进阶知识

UGUI - Events, iTween Plugin
随机推荐
Binary tree --- heap
硬件工程师90天学习资料及笔记汇总20220730
PHP笔记 28 29 30 31
组合数模板
进程管理(动态的)
Uni-app开发微信小程序使用本地图片做背景图
时序动作定位 | ACGNet:弱监督时序动作定位的动作补充图网络(AAAI 2022)
js函数聚合的三种实现方式
JS reduce
物联网时代下的网络整合推广外包精准化效果-深圳双赢世讯
day16--The use of the packet capture tool Charles
IDLE development wordCount program (5)
【Unity入门计划】Collision2D类&Collider2D类
PTA 习题2.2 数组循环左移
明明加了唯一索引,为什么还是产生重复数据?
LaTeX出现错误代码Command \algorithmic already defined
Go-Excelize API source code reading (11) - GetActiveSheetIndex()
Day37 LeetCode
Rust learning: 6.1_Slices of composite types
第十六天&charles的基本操作