# 题目

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 。

示例二:

输入: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 。

示例 3:

1 <= nums1.length <= nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 104
nums1和nums2中所有整数 互不相同
nums1 中的所有整数同样出现在 nums2 中

tips:

  • 1 <= nums1.length <= nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 104
  • nums1 和 nums2 中所有整数 互不相同
  • nums1 中的所有整数同样出现在 nums2 中

# 解法一:使用栈

拿到这个题毫无头绪,理解有误,用了双层循环试图暴力,但是不是和 nums2 中同一个数的下一位作比较。是找 nums2 中该元素的下一个第一个最大元素,突破口在于 nums1 是 nums2 的子集,可以利用栈和 map 将 nums2 中每一位的下一位最大存起来,然后遍历 num1,返回结果。

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] res = new int[nums1.length];
        Stack<Integer> stack = new Stack();
        HashMap<Integer, Integer> map = new HashMap();
        for(int i : nums2){
            while(!stack.isEmpty() && stack.peek() < i){
                map.put(stack.pop(), i);
            }
            stack.push(i);
        }
        for(int i = 0; i < nums1.length; i++){
            res[i] = map.getOrDefault(nums1[i], -1); 
        }
        return res;
    }
}

# 解法二: 暴力解题

1. 初始化和 nums1 等长的数组;

2. 遍历 nums1 中的所有元素,不妨设当前遍历的元素为 nums1 [i];

  • 从前向后遍历 nums2 中的元素,直至找到 nums2 [j] = nums1 [i];
  • 从 j+1 开始继续遍历,直至找到 nums2 [k] >nums2 [j], 其中 k>j+1;
  • 如果找到,存入 nums2 [k+1],否则 - 1

3.res 为最终结果

当时想着要循环三次 就直接放弃了,为什么不尝试暴力解法呢?

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int m = nums1.length, n = nums2.length;
        int[] res = new int[m];
        for (int i = 0; i < m; ++i) {
            int j = 0;
            while (j < n && nums2[j] != nums1[i]) {
                ++j;
            }
            int k = j + 1;
            while (k < n && nums2[k] < nums2[j]) {
                ++k;
            }
            res[i] = k < n ? nums2[k] : -1;
        }
        return res;
    }
}

我的暴力:

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] res = new int[nums1.length];
        int k = 0, j = 0;
        for(int i = 0 ; i <nums1.length; i++){
            int num = nums1[i];
            while(j < nums2.length){
                if(num == nums2[j]){
                    k = j+1;
                    break;
                }else {
                    res[i] = -1;
                }
                j++;
            }
            if(k == 0){
                break;
            }
            while(k < nums2.length){
                if(nums2[k] > num){
                    res[i] = nums2[k];
                    j = 0;k= 0;
                    break;
                }else {
                    res[i] = -1;
                }
                 k++;
            }
             j = 0;k= 0;
        }
        return res;
    }
}