Python Solution for Beginners

  • 0

    I've yet to take an algorithm course so here's my solution to the problem. Please note that the question asks to find the NGE in the second list (nums) starting at the index of the findNums element in second list. Meaning: 2 (from [1,4,2]) is in index 3 of nums ([1,3,4,2]), but since there is no greater number than 2 to the right of that position, -1 is returned. Enjoy!

    def nextGreaterElement(findNums, nums):
            ans = []
            i = 0
            while len(ans) != len(findNums):
                x = findNums[i] #element
                y = nums.index(x) #finds index in nums
                i = i + 1
            return ans
    def check(x, lst):
        for ele in lst:
            if ele > x:
                return ele
        return -1

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.