← 返回 waymo 的题目列表Minimum Index Distance Between 1 and 2
类型:online_judge
Given an array nums of length n where each element is only 0, 1, or 2.
Return the minimum absolute index distance between any pair of indices (i, j) such that nums[i] = 1 and nums[j] = 2 (order does not matter). Formally:
min |i - j| where nums[i]=1 and nums[j]=2.
If there is no 1 or no 2 in the array, return -1.
Constraints:
1 <= n <= 2e5
Examples:
Input: [0,1,0,2,2] Output: 2
Input: [0,0,1,0] Output: -1
Example
Input
5
0 1 0 2 2
Output
2