[LeetCode]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.

难度

Easy

方法1

直接按照题目意思查找,时间复杂度O(m*n)

python代码
class Solution(object):
    def nextGreaterElement(self, findNums, nums):
        """
        :type findNums: List[int]
        :type nums: List[int]
        :rtype: List[int]
        """
        result = []
        for findNum in findNums:
            numsCount = len(nums)
            i = 0
            equalFlag = False
            while i < numsCount:
                if equalFlag:
                    if findNum < nums[i]:
                        result.append(nums[i])
                        break;
                    else:
                        i += 1
                else:
                    if findNum == nums[i]:
                        equalFlag = True
                    i += 1
            if i == numsCount:
                result.append(-1)

        return result

assert Solution().nextGreaterElement([4,1,2], [1,3,4,2]) == [-1,3,-1]
assert Solution().nextGreaterElement([2,4], [1,2,3,4]) == [3,-1]
方法2

利用map和stack,遍历nums2,将每个元素和它next greater element的对应关系存入map中,最后遍历nums1从map中取值,如果没有返回-1。时间复杂度O(m+n)

python代码
class Solution(object):
    def nextGreaterElement(self, findNums, nums):
        """
        :type findNums: List[int]
        :type nums: List[int]
        :rtype: List[int]
        """
        numsStack = []
        numsMap = {}

        for num in nums:
            while len(numsStack) and (numsStack[-1] < num):
                numsMap[numsStack.pop()] = num
            numsStack.append(num)

        result = []
        for findNum in findNums:
            result.append(numsMap.get(findNum, -1))

        return result

assert Solution().nextGreaterElement([4,1,2], [1,3,4,2]) == [-1,3,-1]
assert Solution().nextGreaterElement([2,4], [1,2,3,4]) == [3,-1]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Example 1: Input:nums1= [4,1,2],nums2= [1,3,4,2]. Output:...
    KkevinZz阅读 564评论 0 0
  • 小时候,悦悦背过三字经,背过一点点笠翁对韵,后来不了了之了。第三天,看《这就是二十四节气•春》:清明,教了杜牧的《...
    悦悦和书的那些事阅读 509评论 0 0
  • 我在暮色四合的街头不等风不等雨不等天黑,只等一个人。我不知他姓名,不知他容貌,甚至不识他声色,他也许下一刻抵达,我...
    鹿鳴YAN阅读 403评论 0 1
  • 忙碌的十月... 去西藏回来已经一个多月了,一回来就忙着复习,忙着考试,忙着培训,忙着工作......我好...
    Jing4520阅读 541评论 2 1
  • 985大一学生死于酒吧的消息一时间传的沸沸扬扬。每个人 所持的态度也不一样。新浪上有人留言“我说这是大学,又不是保...
    烟花雨荨阅读 285评论 0 0

友情链接更多精彩内容