ntree.ai
ntree.aiLocal Minimum

Local Minimum

mediumsolvedattempted

Given an array nums, return the index of any local minimum.

A local minimum is a number that is less than both of its neighbours, counting a missing neighbour as greater.

All elements in the array are unique.

Example 1:

Input: nums = [9, 7, 4, 2, 5, 8]
Output: 3
Explanation: nums[3] = 2, 2 < 4 and 2 < 5.

Example 2:

Input: nums = [1, 2, 3]
Output: 0
Explanation: nums[0] = 1, 1 < 2.

Constraints:

  • 1 <= length of nums <= 10^5
  • -10^9 <= nums[i] <= 10^9
def findLocalMinimum(nums: list[int]) -> int:
pass
 

Press Visualise to step through your code.