LeetCode 题解工作台
下一个更大元素 I
nums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。 给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中 nums1 是 nums2 的子集。 对于每个 0 ,找出满足 nums1[i] == n…
4
题型
7
代码语言
3
相关题
当前训练重点
简单 · 数组·哈希·扫描
答案摘要
我们可以从右往左遍历数组 ,维护一个从栈顶到栈底单调递增的栈 ,并且用哈希表 记录每个元素的下一个更大元素。 遍历到元素 时,如果栈不为空且栈顶元素小于 ,我们就不断弹出栈顶元素,直到栈为空或者栈顶元素大于等于 。此时,如果栈不为空,栈顶元素就是 的下一个更大元素,否则 没有下一个更大元素。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路
题目描述
nums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。
给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中nums1 是 nums2 的子集。
对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j] 的 下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1 。
返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素 。
示例 1:
输入:nums1 = [4,1,2], nums2 = [1,3,4,2]. 输出:[-1,3,-1] 解释:nums1 中每个值的下一个更大元素如下所述: - 4 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。 - 1 ,用加粗斜体标识,nums2 = [1,3,4,2]。下一个更大元素是 3 。 - 2 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。
示例 2:
输入:nums1 = [2,4], nums2 = [1,2,3,4]. 输出:[3,-1] 解释:nums1 中每个值的下一个更大元素如下所述: - 2 ,用加粗斜体标识,nums2 = [1,2,3,4]。下一个更大元素是 3 。 - 4 ,用加粗斜体标识,nums2 = [1,2,3,4]。不存在下一个更大元素,所以答案是 -1 。
提示:
1 <= nums1.length <= nums2.length <= 10000 <= nums1[i], nums2[i] <= 104nums1和nums2中所有整数 互不相同nums1中的所有整数同样出现在nums2中
进阶:你可以设计一个时间复杂度为 O(nums1.length + nums2.length) 的解决方案吗?
解题思路
方法一:单调栈
我们可以从右往左遍历数组 ,维护一个从栈顶到栈底单调递增的栈 ,并且用哈希表 记录每个元素的下一个更大元素。
遍历到元素 时,如果栈不为空且栈顶元素小于 ,我们就不断弹出栈顶元素,直到栈为空或者栈顶元素大于等于 。此时,如果栈不为空,栈顶元素就是 的下一个更大元素,否则 没有下一个更大元素。
最后我们遍历数组 ,根据哈希表 得到答案。
时间复杂度 ,空间复杂度 。其中 和 分别为数组 和 的长度。
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stk = []
d = {}
for x in nums2[::-1]:
while stk and stk[-1] < x:
stk.pop()
if stk:
d[x] = stk[-1]
stk.append(x)
return [d.get(x, -1) for x in nums1]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidates should be able to explain how the stack is used to maintain the next greater elements in a monotonic manner.
- question_mark
Look for a clear understanding of how hash tables optimize the lookup process after processing nums2.
- question_mark
Candidates should discuss the time and space complexities and how they are optimized compared to a brute-force solution.
常见陷阱
外企场景- error
Not using the stack to maintain the next greater elements in a monotonic order, which leads to a less efficient solution.
- error
Overlooking the need to map elements of nums2 to their next greater element using a hash table, leading to slower lookups.
- error
Misunderstanding the problem constraints, leading to inefficient or incorrect handling of array indices.
进阶变体
外企场景- arrow_right_alt
Handling multiple queries for different nums1 arrays with the same nums2 array.
- arrow_right_alt
Optimizing further using additional data structures, like trees or heaps, to support different types of range queries.
- arrow_right_alt
Adapting the approach for finding the previous greater element instead of the next greater element.