Given a 1-d space, represented as an int array.
Each element in this array can have one of the three values {0, 1, 2} with meanings:
0— space is empty1— there is a person at this space2— there is a cake at this space
The distance between a cake and a person is defined as the number of spaces between them.
Please write a method to get the minimum distance between any person and any cake in the space.
Example:1 0 2 0 0 → Answer: 1
You are given a 1D array containing people and cakes. You must compute the minimum distance between any person (1) and any cake (2). Simply scan the array, record all positions of 1 and 2, and compute the smallest absolute difference between them.
The VOprep team has long accompanied candidates through various major company OAs and VOs, including SnowFlake, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for these companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.