Pinterest OA 面试真题解析:Find the Special Interest in Each Board | 数组计数

18次阅读
没有评论

Find the Special Interest in Each Board

You are scrolling through Pinterest and stumble upon a collection of boards. Each board is organized such that it has an even number of pins and at least 6 pins. The boards are provided as a list of lists.

Each sublist contains exactly three different possible integers, each representing the interest of the pin: a default interest that appears half the time, an outlier interest that appears exactly once, and a special interest that appears the rest of the time.

Write a function that takes in the list of lists of interests of the pins and returns a list with the special interest from each sublist.

Sample input/output:

boards = [[1, 2, 9, 9, 9, 2],
    [15, 14, 13, 14, 14, 15, 14, 15],
    [5, 5, 6, 6, 6, 7],
    [10, 8, 10, 8, 8, 11, 10, 10],
]

find_special_interests(boards) => [2, 15, 5, 8]

这道题的核心是从每个子数组中找出出现次数最多、但不是“默认值”或“仅出现一次的异常值”的那个兴趣值。由于每个 board 只包含三种不同数字,且它们的出现次数模式固定,可以直接用哈希计数统计每个数字的频率,再找出满足题意的“special interest”。整体思路非常适合用字典 /Counter,时间复杂度为 O(n),其中 n 为所有子数组元素总数,空间复杂度取决于每个子数组的不同元素个数。

正文完
 0