github repo 地址: https://github.com/GoldenaArcher/js_leetcode,Github 的目录 大概 会更新的更勤快一些。
题目地址:496. Next Greater Element I
如下:
The next greater element of some element
x
in an array is the first greater element that is to the right ofx
in the same array.You are given two distinct 0-indexed integer arrays
nums1
andnums2
, wherenums1
is a subset ofnums2
.For each
0 <= i < nums1.length
, find the indexj
such thatnums1[i] == nums2[j]
and determine the next greater element ofnums2[j]
innums2
. If there is no next greater element, then the answer for this query is-1
.Return an array
ans
of lengthnums1.length
such thatans[i]
is the next greater element as described above.
easy 的题目暴力解可用:遍历 nums1
并找到 nums2
中比它大的数字,这个的时间复杂度最大为
O
(
n
2
)
O(n^2)
O(n2)。
另一种解法是使用单调栈去解决。
以下面两个数组作为例子:num1 = [1, 3, 8], nums2 = [3, 2, 1, 8]
,其核心思想就是在遇到 stack[stack.length - 1] > curr
的情况下,将值持续性的压入栈中。当遇到 stack[stack.length - 1] < curr
时进行弹栈操作。
在这个前提下,栈中的最初几个遍历值为 [3, 2, 1]
,当遇到 8 时,就可以进行弹栈操作。对于 1
, 2
, 3
来说,下一个最大数字都是 8
. 随后将 8
压入栈中,此时数组完成遍历,不可能存在比 8 更大的数字,因此,将 nums
中 8
所在的下标标记为 -1
。
这样每次弹出的数值一定是一个递增或是递减的状态,留在栈中的值也会呈现一个递增或递减的状态,这样的设计结构在寻找下一个最大/最小值时最为适用。
单调栈的模板为:
for (let i = 0; i < arr.length; i++) {
const curr = arr[i];
while (stack.length && stack[stack.length - 1] > curr) {
stack.pop();
}
stack.push(curr);
}
var nextGreaterElement = function (nums1, nums2) {
const res = [];
let found, nextMinIdx;
for (let i = 0; i < nums1.length; i++) {
const num = nums1[i];
found = false;
nextMinIdx = -1;
for (let j = 0; j < nums2.length; j++) {
if (nums2[j] === nums1[i]) {
found = true;
nextMinIdx = j;
break;
}
}
for (let j = nextMinIdx; j < nums2.length; j++) {
if (nums2[j] > nums2[nextMinIdx]) {
res.push(nums2[j]);
break;
}
}
if (res.length !== i + 1) res.push(-1);
}
return res;
};
var nextGreaterElement = function (nums1, nums2) {
const res = [];
let found, nextMinIdx;
const map = {};
for (let i = 0; i < nums1.length; i++) {
map[nums1[i]] = i;
}
const stack = [];
for (let i = 0; i < nums2.length; i++) {
const curr = nums2[i];
while (stack.length && curr > stack[stack.length - 1]) {
const val = stack.pop();
if (map[val] !== undefined) {
res[map[val]] = curr;
}
}
stack.push(curr);
}
for (const val of stack) {
if (map[val] !== undefined) res[map[val]] = -1;
}
return res;
};