当前位置:网站首页>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;}}
边栏推荐
- The implementation of the seemingly useless component (text gradient) in NaiveUI is so simple
- raid5的写性能,是不的比raid10快一些?
- IDLE development wordCount program (5)
- Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen
- 90. (cesium house) cesium height monitoring events
- Linux下载安装MySql
- Rust learning: 6.3_ Tuples of composite types
- qrcode-----生成二维码
- ShardingSphere入门
- Relaxation class: the boss will martial arts, who also can not hold up against!The charm of six sigma training
猜你喜欢

Linux下载安装MySql

It is obvious that a unique index is added, why does it still generate duplicate data?

Rust学习:6.1_复合类型之切片

第十六天&charles的基本操作
深度剖析“八大排序”(上)_ 探寻一些不为人知的细节

【Unity入门计划】2D游戏实现敌人来回移动控制脚本

详解构建mock服务最方便的神器——Moco

CV+Deep Learning——网络架构Pytorch复现系列——classification(三:MobileNet,ShuffleNet)

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

Introduction to C integer data storage
随机推荐
菜鸟、小白在autojs和冰狐智能辅助之间如何选择?
Day36 LeetCode
时序动作定位 | ACGNet:弱监督时序动作定位的动作补充图网络(AAAI 2022)
Summary of ctfshow SSTI knowledge points
基于sklearn的决策树应用实战
深度剖析“八大排序”(上)_ 探寻一些不为人知的细节
winget包管理器
每日一题,数组字符串的匹配问题
同步锁synchronized追本溯源
【业务架构】价值链分析:提高客户价值和盈利能力
全连接神经网络结构图,神经网络示意图怎么画
怎么使用【jmeter正则表达式提取器】解决返回值作参数的问题
如何远程调试对方的H5页面
How AliExpress sellers seize product search weight
Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen
人工神经网络模型的特点,人工神经网络模型定义
[Learn Rust together | Advanced articles | RMQTT library] RMQTT message server - installation and cluster configuration
【OAuth2】十九、OpenID Connect 动态客户端注册
nrm 使用详解
1499. 满足不等式的最大值 堆/双端队列