Ones and Zeroes

MediumDynamic Programming2d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an array of binary strings strs and two budgets m and n. Choose a subset of the strings (each string at most once) such that, across the chosen strings, the total number of '0' characters is at most m and the total number of '1' characters is at most n. Return the size of the largest such subset.

Examples

Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3
Output: 4
Pick "10", "0001", "1", "0": 5 zeros and 3 ones.
Input: strs = ["10","0","1"], m = 1, n = 1
Output: 2
Pick "0" and "1".

Constraints

  • 1 <= strs.length <= 600
  • 1 <= strs[i].length <= 100
  • `strs[i]` consists only of `'0'` and `'1'`
  • 1 <= m, n <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 2d dp problems