Python code that almost works. Doesn't work for test case 51. Can anyone spot the problem?


  • 0
    Y
    class Solution:
    # @param num, a list of integer
    # @return an integer
    def findPeakElement(self, num):
        n=len(num)
        i=0
        greatestVar=num[0]
        greatestIndex=0
        while i<n-1:
            if greatestVar>num[i]:
                i=i+1
            elif n==1:
                greatestIndex=0
            else:
                greatestVar=num[i]
                greatestIndex=i
                i=i+1
        return greatestIndex

  • 0
    J

    Why n-1 in loop condition? Then i has the range from 0 to n-2.


Log in to reply
 

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