← 返回 snowflake 的题目列表Find the nearest cake in a 1D array
类型:online_judge
Given an integer array A and an integer start, where A[i] indicates whether there is a cake at position i. Find the minimum number of moves required to reach the nearest cake from the position start.
Input:
A: An integer array, only containing 0 and 1, where 0 means there is no cake at the position and 1 means there is a cake.
start: An integer representing the starting position.
Output:
An integer representing the minimum number of moves required to reach the nearest cake from start.
Example:
Input:
A = [0, 0, 1, 0, 0, 1, 0]
start = 3
Output:
2
Explanation: The nearest cake is at index 2, which requires 2 moves.
Example
Input
A = [0, 0, 1, 0, 0, 1, 0], start = 3