`

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]

分享到:
评论

相关推荐

    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

    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...

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

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

    折半查找 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 ...

    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 ...

    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 -...

    occam一维反演

    C OF THE MODEL, SUCH AS LAYER THICKNESSES, FINITE ELEMENT MESH, ETC. COMMUNICATION C BETWEEN INPUTM() AND FORMOD() AND FORDIV() MUST BE VIA THE USER'S OWN COMMON C BLOCKS. WITH STATIC COMMONS (CHECK ...

    2009 达内Unix学习笔记

    mv [-f] [-i] f1 ... fn d1 mv [-f] [-i] d1 d2 mv 源文件名 目标文件名 若目标文件名还没有,则是源文件重命名为目标文件;若目标文件已存在,则源文件覆盖目标文件。 mv 源文件名 目标目录 移动文件 mv 源目录...

Global site tag (gtag.js) - Google Analytics