Python solution exceeds time limits

  • 0

    My Python solution fails at the [1,2,3,2,1] test case, where I believe it should return the answer in no time (shouldn't even get into the while loop). However, the time limit was exceeded.

    What's wrong with my code?

    class Solution:
        # @param nums, an integer[]
        # @return an integer
        def findPeakElement(self, nums):
            if len(nums) < 3:
                return nums[0] if len(nums) == 1 else max(nums[0], nums[1])
            mid = nums[len(nums)/2]
            while mid != len(nums)-1 and mid != 0 and (nums[mid] < nums[mid-1] or nums[mid] < nums[mid+1]):
                mid = (mid + len(nums))/2 if nums[mid] < nums[mid+1] else mid/2
            return mid

  • 0

    The question asks for:
    find a peak element and return its index

    Your code returns the value instead

Log in to reply

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