496、503、下一个更大的元素

  1. 496
    1. 示例 1:
    2. 示例 2:
  2. 分析
    1. 优化 哈希表
  3. 503
    1. 示例 1:
    2. 示例 2:
  4. 分析
    1. 模运算
    2. 单调栈算法

496

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 <= 1000
  • $0 <= nums1[i], nums2[i] <= 10^4$
  • nums1和nums2中所有整数 互不相同
  • nums1 中的所有整数同样出现在 nums2 中

分析

按照题目模拟:

  • 遍历num1
    • 先在nums2找到相同元素
    • 继续遍历num2,找到第一个更大的元素
  • 返回结果数组
class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int n1 = nums1.length;
        int n2 = nums2.length;
        for (int i = 0;i < n1;i++) {
            int j = 0;
            while (j < n2 && nums1[i] != nums2[j]) {
                j++;
            }
            j++;
            while(j < n2 && nums2[j] <= nums1[i] ) {
                j++;
            }
            if (j == n2) {
                nums1[i] = -1;
            } else {
                nums1[i] = nums2[j];
            }
        }

        return nums1;
    }
}

优化 哈希表

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int n1 = nums1.length;
        int n2 = nums2.length;
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0;i < n2;i++) {
            map.put(nums2[i],i);
        }
        for (int i = 0;i < n1;i++) {
            int j = map.getOrDefault(nums1[i], -1) + 1;
            while(j != -1 && j < n2 && nums2[j] <= nums1[i]) {
                j++;
            } 
            if (j == -1 || j == n2) {
                nums1[i] = -1;
            } else {
                nums1[i] = nums2[j];
            }
        }

        return nums1;
    }
}

503

给定一个循环数组 nums ( nums[nums.length - 1] 的下一个元素是 nums[0] ),返回 nums 中每个元素的 下一个更大元素 。

数字 x 的 下一个更大的元素 是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1 。

示例 1:

输入: nums = [1,2,1]
输出: [2,-1,2]
解释: 第一个 1 的下一个更大的数是 2;
数字 2 找不到下一个更大的数; 
第二个 1 的下一个最大的数需要循环搜索,结果也是 2。

示例 2:

输入: nums = [1,2,3,4,3]
输出: [2,3,4,-1,4]

提示:

  • $1 <= nums.length <= 10^4$
  • $-10^9 <= nums[i] <= 10^9$

分析

模运算

class Solution {
    public int[] nextGreaterElements(int[] nums) {
        int n = nums.length;
        int[] res = new int[n];
        for (int i = 0;i < n;i++) {
            int j = i+1;
            while(nums[j % n] <= nums[i] && j % n != i) {
                j++;
            }
            if (j % n == i) {
                res[i] = -1;
            } else {
                res[i] = nums[j % n];
            }
        }

        return res;
    }
}

单调栈算法

class Solution {
    public int[] nextGreaterElements(int[] nums) {
        int n = nums.length;
        int[] ret = new int[n];
        Arrays.fill(ret, -1);
        // 栈存储还没有找到第一个更大的元素的元素的下标
        Deque<Integer> stack = new LinkedList<Integer>();
        for (int i = 0; i < n * 2 - 1; i++) {
            // 栈非空,如果栈顶下标元素小雨当前元素(),持续出栈
            while (!stack.isEmpty() && nums[stack.peek()] < nums[i % n]) {
                // 巧妙之处,给栈中的下标的元素进行赋值,完成ret的创建
                ret[stack.pop()] = nums[i % n];// 找到了第一个更大的元素
            }
            // 保存当前元素的下标
            stack.push(i % n);
        }
        return ret;
    }
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1056615746@qq.com

💰

Title:496、503、下一个更大的元素

Count:1.1k

Author:攀登

Created At:2024-06-24, 13:13:39

Updated At:2024-06-24, 13:49:46

Url:http://jiafeimao-gjf.github.io/2024/06/24/496%E3%80%81503%E3%80%81%E4%B8%8B%E4%B8%80%E4%B8%AA%E6%9B%B4%E5%A4%A7%E7%9A%84%E5%85%83%E7%B4%A0/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

×

Help us with donation