Snowflake 面试题:一维数组中“人”和“蛋糕”的最小距离(含题目解析)

77次阅读
没有评论

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 empty
  • 1 — there is a person at this space
  • 2 — 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

给你一个一维数组,里面有“人 (1)”和“蛋糕 (2)”。要求找到任意一个人和任意一个蛋糕之间的“最小距离”。做法就是找出所有 1 和 2 的索引,然后算它们之间的最小绝对差值即可。

VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 SnowFlake、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。

正文完
 0