LeetCode 题解工作台

满足不等式的最大值

给你一个数组 points 和一个整数 k 。数组中每个元素都表示二维平面上的点的坐标,并按照横坐标 x 的值从小到大排序。也就是说 points[i] = [x i , y i ] ,并且在 1 的前提下, x i j 总成立。 请你找出 y i + y j + |x i - x j | 的 最大…

category

5

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · 滑动窗口(状态滚动更新)

bolt

答案摘要

题目要求 $y_i + y_j + |x_i - x_j|$ 的最大值,其中 $i \lt j$,并且 $|x_i - x_j| \leq k$。由于 是严格单调递增的,那么: $$

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 滑动窗口(状态滚动更新) 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个数组 points 和一个整数 k 。数组中每个元素都表示二维平面上的点的坐标,并按照横坐标 x 的值从小到大排序。也就是说 points[i] = [xi, yi] ,并且在 1 <= i < j <= points.length 的前提下, xi < xj 总成立。

请你找出 yi + yj + |xi - xj|最大值,其中 |xi - xj| <= k1 <= i < j <= points.length

题目测试数据保证至少存在一对能够满足 |xi - xj| <= k 的点。

 

示例 1:

输入:points = [[1,3],[2,0],[5,10],[6,-10]], k = 1
输出:4
解释:前两个点满足 |xi - xj| <= 1 ,代入方程计算,则得到值 3 + 0 + |1 - 2| = 4 。第三个和第四个点也满足条件,得到值 10 + -10 + |5 - 6| = 1 。
没有其他满足条件的点,所以返回 4 和 1 中最大的那个。

示例 2:

输入:points = [[0,0],[3,0],[9,2]], k = 3
输出:3
解释:只有前两个点满足 |xi - xj| <= 3 ,代入方程后得到值 0 + 0 + |0 - 3| = 3 。

 

提示:

  • 2 <= points.length <= 105
  • points[i].length == 2
  • -108 <= xi, yi <= 108
  • 0 <= k <= 2 * 108
  • 对于所有的 1 <= i < j <= points.length,均有 xi < xj
  • xi 构成一个严格递增序列。
lightbulb

解题思路

方法一:优先队列(大根堆)

题目要求 yi+yj+xixjy_i + y_j + |x_i - x_j| 的最大值,其中 i<ji \lt j,并且 xixjk|x_i - x_j| \leq k。由于 xix_i 是严格单调递增的,那么:

yi+yj+xixj=yi+yj+xjxi=(yixi)+(xj+yj)\begin{aligned} y_i + y_j + |x_i - x_j| & = y_i + y_j + x_j - x_i \\ & = (y_i - x_i) + (x_j + y_j) \end{aligned}

因此,对于当前遍历到的点 (xj,yj)(x_j, y_j),我们只需要找到前面所有满足 xjxikx_j - x_i \leq k 的点 (xi,yi)(x_i, y_i)yixiy_i - x_i 的最大值,再加上当前的 xj+yjx_j + y_j 即可。而 yixiy_i - x_i 的最大值,我们可以使用优先队列(大根堆)来维护。

具体地,我们定义一个优先队列(大根堆) pqpq,堆中每个元素是一个二元组 (yixi,xi)(y_i - x_i, x_i)

当我们遍历到点 (x,y)(x, y) 时,如果堆 pqpq 不为空,并且 xpq[0][1]>kx - pq[0][1] \gt k,那么循环将堆顶元素弹出,直到堆为空或者满足 xpq[0][1]kx - pq[0][1] \leq k。此时,堆顶元素 (yixi,xi)(y_i - x_i, x_i) 即为所有满足 xjxikx_j - x_i \leq k 的点中 yixiy_i - x_i 的最大值,此时更新答案 ans=max(ans,x+y+pq[0][0])ans = \max(ans, x + y + pq[0][0])

然后,我们将点 (x,y)(x, y) 加入堆中,继续遍历下一个点,直到遍历完整个数组 pointspoints

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(n)O(n)。其中 nn 是数组 pointspoints 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
        ans = -inf
        pq = []
        for x, y in points:
            while pq and x - pq[0][1] > k:
                heappop(pq)
            if pq:
                ans = max(ans, x + y - pq[0][0])
            heappush(pq, (x - y, x))
        return ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for an understanding of how to maintain a sliding window.

  • question_mark

    Check if the candidate correctly uses a priority queue for efficient lookups.

  • question_mark

    Evaluate if the candidate handles the constraint |xi - xj| <= k appropriately while iterating.

warning

常见陷阱

外企场景
  • error

    Failing to properly manage the sliding window size could lead to incorrect results.

  • error

    Not using the priority queue effectively might result in inefficient or incorrect computation of the maximum value.

  • error

    Ignoring the constraint |xi - xj| <= k could cause invalid pairs to be considered.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Increase the problem's complexity by adding additional constraints on the y-values of points.

  • arrow_right_alt

    Consider modifying the equation to include additional terms or weights for each point.

  • arrow_right_alt

    Allow for dynamic updates to the points array and re-calculate the maximum value accordingly.

help

常见问题

外企场景

满足不等式的最大值题解:滑动窗口(状态滚动更新) | LeetCode #1499 困难