Unicorns OA 面试真题解析:Video Game Attack and Game Over

17次阅读
没有评论

We are trying to create a video game in which aliens are attacking a friendly base. You can imagine the base to be a 1D line that goes from 0 to 10.

The alien’s attack length is always 1, but it can choose to attack anywhere along the base. We would like to implement two functions for the video game: attack and is_game_over.

For the attack function, it takes a number from 0 to 10 and should mark the corresponding place to start the attack. For example, if attack is called with 3, then the line subset 3-4 should be marked as destroyed.

For the is_game_over function, it should return a boolean, which specifies whether or not the entire base has been destroyed.

这道题本质上是在一条长度为 10 的一维区间上做区间标记:`attack(x)` 会把从 `x` 开始长度为 1 的区间(即 `[x, x+1)`)标记为已摧毁,而 `is_game_over()` 需要判断整条基地是否已经全部被覆盖。实现时可以用布尔数组或位图记录每个单位区间是否被攻击过,再在查询时检查是否所有位置都已被标记;如果希望更高效,也可以维护已摧毁的连续覆盖状态,避免重复统计。

正文完
 0