Save the half which has result

162 Find the peak element

A peak element is an element that is greater than its neighbors.

Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.

The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.

You may imagine that nums[-1] = nums[n] = -∞.

Define peak A[P] > A[P-1] && A[P] > A[P+1]

def findPeakElement(self, nums: List[int]) -> int:
    l, r = 0, len(nums)-1
    while l < r:
        mid = (l+r)//2
        if nums[mid] > nums[mid+1]:
            r = mid
        else:
            l = mid+1
            
    return l

222. Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes.

Can you solve in O(d) time

Last updated

Was this helpful?