It's a variant of a known LeetCode problem but the twist of returning answers for every K from 1 to n in one shot is what killed me.
Model the problem as a min-cost selection where each person can cover one or both skills. Precompute the cheapest costs for each skill category (only skill A, only skill B, both skills), then combine them efficiently to find the minimum cost for each K. Use sorting and prefix sums or dynamic programming to compute the answers for all K.
Pro tip: Clarify the constraints upfront (e.g., number of people, cost range) to choose the right algorithm; often a greedy approach with sorted lists works after separating people by skill sets.
Restate the problem in your own words and ask clarifying questions about input size, cost ranges, and whether people can be selected multiple times. This ensures you design an efficient solution.
Separate people into three groups: those with only skill A, only skill B, and both skills. Sort each group by cost ascending.
For each group, compute prefix sums of costs so you can quickly get the total cost of selecting the cheapest i people from that group.
For each possible number of people with both skills (from 0 to K), determine how many from the single-skill groups are needed to cover the remaining K. Use the prefix sums to compute the total cost and take the minimum.
If for some K it's impossible to select enough people, set the answer to -1. Return the array of minimum costs for K from 0 to the maximum possible.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.