首页 > 其他分享 >leetcode-496-easy

leetcode-496-easy

时间:2023-01-08 19:57:29浏览次数:47  
标签:map int nums1 length result easy 496 leetcode nums2

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 of x in the same array.

You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.

For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. 
If there is no next greater element, then the answer for this query is -1.

Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2]
Output: [-1,3,-1]
Explanation: The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4]
Output: [3,-1]
Explanation: The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.
Constraints:

1 <= nums1.length <= nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 104
All integers in nums1 and nums2 are unique.
All the integers of nums1 also appear in nums2.
Follow up: Could you find an O(nums1.length + nums2.length) solution?

思路一:用 map 存储 nums2 的值和下标,然后遍历 nums1 求解

    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] result = new int[nums1.length];
        Arrays.fill(result, -1);

        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums2.length; i++) {
            map.put(nums2[i], i);
        }

        for (int j = 0; j < nums1.length; j++) {
            int num = nums1[j];
            for (int i = map.get(num) + 1; i < nums2.length; i++) {
                if (nums2[i] > num) {
                    result[j] = nums2[i];
                    break;
                }
            }
        }

        return result;
    }

思路二:看了题解,发现有更好的解法,用单调栈求解

for(int i = nums2.length - 1; i >= 0; i--){
    while(!s.empty() && s.peek() <= nums2[i]){//栈顶只要小于待入栈元素就不断出栈,循环退出条件是栈顶元素大于nums2[i],这样在入栈之后才能保持单调栈的情况
        s.pop();//持续出栈,构造单调栈
    }
    map.put(nums2[i],s.empty() ? -1 : s.peek());//map中记录对应的映射
    s.push(nums2[i]);//入栈
}

标签:map,int,nums1,length,result,easy,496,leetcode,nums2
From: https://www.cnblogs.com/iyiluo/p/17035195.html

相关文章

  • leetcode-521-easy
    LongestUncommonSubsequenceIGiventwostringsaandb,returnthelengthofthelongestuncommonsubsequencebetweenaandb.Ifthelongestuncommonsubseq......
  • leetcode-551-easy
    StudentAttendanceRecordIYouaregivenastringsrepresentinganattendancerecordforastudentwhereeachcharactersignifieswhetherthestudentwasab......
  • leetcode-485-easy
    MaxConsecutiveOnesGivenabinaryarraynums,returnthemaximumnumberofconsecutive1'sinthearray.Example1:Input:nums=[1,1,0,1,1,1]Output:3E......
  • leetcode-434-easy
    NumberofSegmentsinaStringGivenastrings,returnthenumberofsegmentsinthestring.Asegmentisdefinedtobeacontiguoussequenceofnon-spacech......
  • 【LeetCode数组#4】长度最小的子数组
    长度最小的子数组力扣题目链接(opensnewwindow)给定一个含有n个正整数的数组和一个正整数s,找出该数组中满足其和≥s的长度最小的连续子数组,并返回其长度。如......
  • [leetcode每日一题]1.8
    ​​2185.统计包含给定前缀的字符串​​难度简单28给你一个字符串数组 ​​words​​ 和一个字符串 ​​pref​​ 。返回 ​​words​​ 中以 ​​pref​​ 作为 ......
  • 【优先队列】LeetCode 23. 合并K个升序链表
    题目链接23.合并K个升序链表思路把全部结点放入优先队列中,然后再依次组成新链表代码classSolution{publicListNodemergeKLists(ListNode[]lists){......
  • LeetCode 236_二叉树的最近公共祖先
    LeetCode236:二叉树的最近公共祖先题目给定一个二叉树,找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树T的两个节点p、q,最近......
  • LeetCode 887. 鸡蛋掉落-题解分析
    题目来源887.鸡蛋掉落题目详情给你k枚相同的鸡蛋,并可以使用一栋从第1层到第n层共有n层楼的建筑。已知存在楼层f,满足 0<=f<=n,任何从高于f的楼层落......
  • 【优先队列】LeetCode 347. 前 K 个高频元素
    题目链接347.前K个高频元素思路前k大模板题代码classSolution{publicint[]topKFrequent(int[]nums,intk){PriorityQueue<Map.Entry<Integer,......