`

496 Next Greater Element I

阅读更多
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
    For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
    For number 1 in the first array, the next greater number for it in the second array is 3.
    For number 2 in the first array, there is no next greater number for it in the second array, so output -1.


Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
    For number 2 in the first array, the next greater number for it in the second array is 3.
    For number 4 in the first array, there is no next greater number for it in the second array, so output -1.


Note:

  1. All elements in nums1 and nums2 are unique.
  2. The length of both nums1 and nums2 would not exceed 1000.


Solution:

[code="python"]class Solution(object):
    def nextGreaterElement(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        hash_map = dict()
        helper = list()
        for s in range(len(nums2)):
            while helper and nums2[s] > helper[-1]:
                hash_map[helper.pop()] = nums2[s]
            helper.append(nums2[s])

        for i in helper:
            hash_map[i] = -1
        return [hash_map.get(i) for i in nums1]

分享到:
评论

相关推荐

    c语言-leetcode题解496-next-greater-element-i.c

    c语言入门 c语言_leetcode题解496-next-greater-element-i.c

    leetcode最大蓄水量-leetcode:记录自己leetocde的过程

    496 Next Greater Element I 复习了 linkedlist,arraylist的区别 2021.2.1 198 House Robber 学会了数组的创建,以及动态规划最基本的题目 2021.2.2 322 兑换钱币 学会了 Arrays.fill 的使用,以及查看源码,返回 ...

    leetcode跳跃-LeetCode:力扣刷题70道!

    leetcode 跳跃 LeetCode 力扣刷题70道! 数组 Array 力扣 485 最大连续1的个数 | Max Consecutive ...Element ...496 下一个更大的元素 | Next Greater Element I 力扣 232 用栈实现队列 | Implement

    java-leetcode题解之第496题下一个更大元素I.zip

    【标题】:本资源主要涉及的是LeetCode编程挑战中的第496题——"Next Greater Element I"(下一个更大元素I)的Java解决方案。这道题目是数据结构和算法领域中常见的一类问题,旨在训练程序员对数组的操作以及对栈这...

    javaScript实现一个单链表,找到单链表中的倒数第n个节点.pdf

    首先,我们需要定义一个`Node`类来表示链表中的每个节点,它包含两个属性:`element`用于存储节点的值,`next`指向下一个节点的引用。 ```javascript class Node { constructor(element) { this.element = ...

    java-leetcode题解之第503题下一个更大元素II.zip

    在LeetCode平台上,第503题名为"下一个更大元素II"(Next Greater Element II)。这是一道关于数组处理和栈的数据结构问题。题目要求给定一个循环有序数组,找到每个元素的下一个更大元素。如果不存在这样的元素,则...

    leetcode1-300.docx

    1. 单调栈的典型用途是找到数组中下一个比自身大的元素(the next greater element, NGE),可以在一次遍历就获得所有元素的 NGE。 这个内容讲解了单调栈的应用场景,即找到数组中下一个比自身大的元素。单调栈是一...

    php-leetcode题解之下一个更大元素.zip

    在这个“php-leetcode题解之下一个更大元素.zip”压缩包中,我们重点关注的是使用PHP语言解决LeetCode上关于“下一个更大元素”(Next Greater Element)的问题。 “下一个更大元素”是一个常见的算法问题,它的目标...

    lrucacheleetcode-leetcode:个人刷leetcode遇到的一些题汇总(golang)

    next-greater-element-ii serialize-and-deserialize-binary-tree subarray-sum-equals-k binary-tree-preorder-traversal n-queens-ii populating-next-right-pointers-in-each-node sum-root-to-leaf-numbers best...

    asp语法大全

    Next [element] ``` `For Each...Next` 用于遍历集合中的每个元素。例如: ```vb Dim arr() = Array("apple", "banana", "cherry") For Each item In arr Response.Write item & " " Next ``` ##### 9. ...

    997leetcodec-myleetcode:我的leetcode解决方案

    496-next-greater-element-ic。 (完毕) 使用优化算法更新 798-smallest-rotation-with-highest-score.c。 (完毕) 使用优化算法更新 456-132-pattern.c。 使用位操作更新 78-subsets.c。 (完毕) 使用动态规划...

    Note of Learning Perl--More Control Structures

    print "var is greater than 5\n"; } else { print "var is less than or equal to 5\n"; } ``` 2. `unless` 语句:与`if`相反,当条件不满足时执行代码块。 3. `if/elsif/else` 链:处理多个可能的情况: ```...

    折半查找 binarySearch

    A binary search finds the median element in a list, compares its value to the one you are searching for, and determines if it’s greater than, less than, or equal to the one you want. A guess that ...

    微机原理编程题集合.doc

    JMP NEXT_ELEMENT NEGATIVE_INC: INC NEGATIVE JMP NEXT_ELEMENT ZERO_INC: INC ZERO NEXT_ELEMENT: INC SI LOOP AGAIN MOV AH,4CH INT 21H CODE ENDS END START ``` 以上程序实现了微机原理中典型的分支程序设计...

    leetcode气温-Stack-1:堆栈-1

    3. **下一个大元素II(Next Greater Element II)**:这道题要求对每个数组元素找到它右侧所有元素中最大的一个,但每个元素只能与它后面的元素进行比较。同样,堆栈可以帮助我们高效地实现这一功能。我们可以遍历...

    STL 源码剖析(侯捷先生译著)

    equal_to, not_equal_to, greater, greater_equal, less, less_equal 7.5 逻辑运算类(Logical)仿函数 422 logical_and, logical_or, logical_not 7.6 证同(identity)、选择(select)、投射(project) 423 ...

    学习

    # Swap if the element found is greater than the next element if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr ``` 在这个代码中,`n`表示数组的长度,外层循环控制遍历的次数,内层...

    kgb档案压缩console版+源码

    model is a distribution over the next character, y_i, given the context of characters seen so far, y_1 ... y_i-1. To simplify coding, PAQ6 uses a binary string alphabet. Thus, the output of a model ...

    STL源码剖析.pdg

    庖丁解牛(侯捷自序) i 目录 v 前言 xvii 本书定位 xvii 合适的读者 xviii 最佳阅读方式 xviii 我所选择的剖析对象 xix 各章主题 xx 编译工具 xx 中英术语的运用风格 xxi 英文术语采用原则 xxii 版面...

    DevExpress VCL 13.2.5 D7-DXE6 FullSource

    •Q583680 - Skin Editor - Changes to the Borders.Left, Borders.Top, and/or Borders.Right property sets are not stored to a skin project if these are the only changes made to a skin element •Q573512 -...

Global site tag (gtag.js) - Google Analytics