LeetCode 题解工作台

最长上升路径的长度

给你一个长度为 n 的二维整数数组 coordinates 和一个整数 k ,其中 0 。 coordinates[i] = [x i , y i ] 表示二维平面里一个点 (x i , y i ) 。 如果一个点序列 (x 1 , y 1 ) , (x 2 , y 2 ) , (x 3 , y 3…

category

3

题型

code_blocks

0

代码语言

hub

3

相关题

当前训练重点

困难 · 二分·搜索·答案·空间

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个长度为 n 的二维整数数组 coordinates 和一个整数 k ,其中 0 <= k < n 。

coordinates[i] = [xi, yi] 表示二维平面里一个点 (xi, yi) 。

如果一个点序列 (x1, y1), (x2, y2), (x3, y3), ..., (xm, ym) 满足以下条件,那么我们称它是一个长度为 m 的 上升序列 :

  • 对于所有满足 1 <= i < m 的 i 都有 xi < xi + 1 且 yi < yi + 1 。
  • 对于所有 1 <= i <= m 的 i 对应的点 (xi, yi) 都在给定的坐标数组里。

请你返回包含坐标 coordinates[k] 的 最长上升路径 的长度。

 

示例 1:

输入:coordinates = [[3,1],[2,2],[4,1],[0,0],[5,3]], k = 1

输出:3

解释:

(0, 0) ,(2, 2) ,(5, 3) 是包含坐标 (2, 2) 的最长上升路径。

示例 2:

输入:coordinates = [[2,1],[7,0],[5,6]], k = 2

输出:2

解释:

(2, 1) ,(5, 6) 是包含坐标 (5, 6) 的最长上升路径。

 

提示:

  • 1 <= n == coordinates.length <= 105
  • coordinates[i].length == 2
  • 0 <= coordinates[i][0], coordinates[i][1] <= 109
  • coordinates 中的元素 互不相同 。
  • 0 <= k <= n - 1
lightbulb

解题思路

方法一

1
2

speed

复杂度分析

指标
时间complexity mainly depends on the binary search iterations multiplied by the DFS or DP over filtered points, which is faster than brute force. Space complexity comes from memoization storage and the temporary filtered list, typically O(n) in the worst case.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Pay attention to filtering coordinates based on both x and y to avoid invalid paths.

  • question_mark

    Consider using binary search over the maximum path length to reduce brute-force attempts.

  • question_mark

    Sorting the coordinates before DFS simplifies subsequence calculations and improves efficiency.

warning

常见陷阱

外企场景
  • error

    Not filtering coordinates strictly less than coordinates[k] leads to invalid paths being counted.

  • error

    Attempting DFS without memoization causes timeouts for large arrays.

  • error

    Confusing increasing path criteria by only checking x or y instead of both.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Compute the longest increasing path without requiring inclusion of a specific point.

  • arrow_right_alt

    Find the longest decreasing path instead, reversing the comparison conditions.

  • arrow_right_alt

    Allow non-strictly increasing sequences and measure the longest weakly increasing path.

help

常见问题

外企场景

最长上升路径的长度题解:二分·搜索·答案·空间 | LeetCode #3288 困难