Meta questions

Points From Distinct Categories

MetaPhone screenMedium

You are given items, a list of [category, points] pairs, and an integer k.

Part 1: Points From Distinct Categories

You are given items, a list of [category, points] pairs, and an integer k. Choose exactly k items so that no two chosen items share a category, and maximise the sum of their points.

Return that maximum sum, or -1 if items has fewer than k distinct categories, or 0 when k is 0.

Points may be negative; even then exactly k items must be chosen.

  1. Example 1
    k
    2
    items
    [[1,10],[2,5],[1,7],[3,8]]
    Output
    18

    Why: Category 1's best item is worth 10, category 2's is 5 and category 3's is 8. The two largest of these are 10 and 8, so the answer is 18. Taking 10 and 7 would reuse category 1.

Constraints

  • 0 <= len(items) <= 10^5
  • -10^9 <= items[i][0] <= 10^9
  • -10^4 <= items[i][1] <= 10^4
  • 0 <= k <= 10^5
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Points From Distinct Categories: Meta interview question | LoopCoach