https://leetcode.com/problems/next-greater-element-i/

Home   »   https://leetcode.com/problems/next-greater-element-i/

# Approach #1
# Time complexity : O(N^2)
# Space complexity : O(N)

class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        ans = [-1] * len(nums1)
        for i in range(len(nums1)):
            j = len(nums2)-1
            while nums2[j] != nums1[i] and j >= 0:
                if nums2[j] > nums1[i]:
                    ans[i] = nums2[j]
                j -= 1
                
        return ans

Leave a Reply

Your email address will not be published.