Earliest Strict Valley in an Integer Sequence
Arrays & HashingAuto-graded · 23 test casesLast updated
Scan an integer list for the leftmost interior value strictly smaller than both adjacent values, returning -1 if none exists, which tests boundary handling and strict comparisons.
You are given a list of non-negative integers. A valley is an element that is strictly smaller than the element directly before it and also strictly smaller than the element directly after it.
Find the valley that appears earliest in the list (lowest index) and return its value.
Function
Implement first_valley(values), where values is a list of integers. It returns a single integer.
Rules
- Only elements that have both a left and a right neighbor can be valleys. That means the first and last elements are never valleys.
- An element at index
iis a valley only ifvalues[i - 1] > values[i]andvalues[i + 1] > values[i]. A neighbor that is merely equal to the element does not count as larger. - If several valleys exist, choose the one with the smallest index (not the one with the smallest value) and return its value.
- If no valley exists, return
-1. This also applies when the list has fewer than 3 elements.
Constraints
1 <= len(values) <= 10^50 <= values[i] <= 10^9- Because every value is non-negative, a result of
-1always means "no valley found".
Examples
Example 1
Input: first_valley([5, 3, 4, 1, 2])
Output: 3
Example 2
Input: first_valley([2, 2, 7, 6, 9, 0])
Output: 6
Example 3
Input: first_valley([1, 2, 2, 3])
Output: -1
Hints
Hint 1
Which indices can possibly have both a left and a right neighbor? Restrict your loop to those.
Hint 2
Use strict comparisons on both sides: an equal neighbor must cause the element to be rejected.
Hint 3
You want the earliest valley by position, so return as soon as you find one instead of collecting them all. Return the value, not the index.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Walk through the interior indices 1 to len(values) - 2 from left to right. At each index, check whether the left neighbor and the right neighbor are both strictly greater than the current element. The first index that passes is the answer, so return its value immediately. If the loop finishes without a match, return -1.
Why it is correct
- Only interior indices can have two neighbors, so restricting the loop to
range(1, len(values) - 1)examines exactly the possible valleys. For lists shorter than 3 elements this range is empty, so-1is returned naturally and no out-of-range or negative-index access occurs. - Strict
>comparisons ensure elements with an equal neighbor are rejected. - Indices are visited in increasing order and the function returns at the first success, so the returned element has the smallest qualifying index. Every earlier index was already checked and failed.
- Values are non-negative, so
-1cannot be confused with a real answer (including a valley whose value is0).
Complexity
- Time: O(n), since each interior index takes a constant number of comparisons and the scan may stop early.
- Space: O(1), since only a loop index is used.
Reference solution
from typing import List
def first_valley(values: List[int]) -> int:
# Only interior indices have two neighbors.
for i in range(1, len(values) - 1):
if values[i - 1] > values[i] < values[i + 1]:
return values[i]
return -1