Cresta VO 面试真题解析:Balanced Sampling from Classes|数组分配|在线评测

12次阅读
没有评论

You have a dataset which contains examples labeled with different classes (single-label, multiple classes); for example, an animal image classification dataset containing different types of animals, one animal per image.

You want to randomly sample an n-element subset from this dataset in such a way that the classes are as balanced as possible, i.e., the difference between the number of examples sampled from each class should be as small as possible.

Write a function which, given n and the number of examples in each class, computes how many examples should be sampled from each class.

这道题本质上是“按类别均衡分配采样数量”的问题。输入总采样数 n 和每个类别的样本上限后,需要把 n 尽量平均地分到各个类别中,同时不能超过每类可用样本数。常见做法是先算出每个类别的基础分配值,再把剩余名额按规则逐个补到还能承载样本的类别里,直到总数满足 n 且各类数量差异尽可能小。实现时通常只需要遍历类别数组,复杂度低,适合面试中的贪心 / 均分分配题型。

正文完
 0