SnowflakeOA #39
Minimum Index Distance Between Person and Cake
估计简单可在线判题
In the array, 0 is an empty spot, 1 is a person, and 2 is a cake. Find the minimum absolute index difference between any person and any cake; return −1 if either kind is missing. This is not about assigning a different cake to each person.
输入格式
The first line contains n, and the second line contains the array. 1≤n≤200000, and elements are only 0, 1, or 2.
输出格式
Output the single integer required by the problem.
样例 1
输入:
5
0 1 0 2 0
输出:
2
样例 2
输入:
3
2 0 1
输出:
2
样例 3
输入:
3
0 0 2
输出:
-1
样例说明
The answers to the three public examples are, in order: 2; 2; -1. The examples were rechecked by independent enumeration, state search, or direct simulation.