← 返回 google 的题目列表Minimum Distance Between People and Cakes
类型:online_judge
Write a method to return the minimum distance between any person and any cake. Inputs are two arrays: one integer array representing people's positions and another representing cakes' positions. Both arrays are sorted in ascending order. Output an integer indicating the minimum distance. Assume people and cakes are on the same dimension. Implement a solution with O(N) time complexity.
Test cases:
People positions: [1, 4, 7], Cake positions: [2, 5]. Output: 1
People positions: [1, 2, 3], Cake positions: [6] Output: 3
People positions: [10, 15, 20], Cake positions: [14, 16, 19] Output: 1
People positions: [1, 1, 1], Cake positions: [1, 1, 1] Output: 0
People positions: [5, 10, 12], Cake positions: [11, 13, 15] Output: 1
Example
Input
[1, 4, 7]
[2, 5]