MediumArrayStringDynamic Programming

Ones and Zeroes

LeetCode
1 approach, code in all languages

You are given an array of binary strings and two integers m and n. Think of m as a budget of zeros and n as a budget of ones that you are allowed to spend.

You want to select a subset of the given strings such that the combined number of zeros across the chosen strings is at most m and the combined number of ones is at most n.

Return the size of the largest such subset that respects both budgets.

Example 1

Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3

Output: 4

The largest subset using at most 5 zeros and 3 ones is {"10","0001","1","0"}, which contains 4 strings.

Example 2

Input: strs = ["10","0","1"], m = 1, n = 1

Output: 2

With one zero and one one available, the best choice is {"0","1"}, giving a subset of size 2.

Constraints

  • 1 <= strs.length <= 600
  • 1 <= strs[i].length <= 100
  • strs[i] consists only of the characters '0' and '1'
  • 1 <= m, n <= 100
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now