|
| 1 | +# [Problem 474: Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +This looks like a knapsack variant: each string has a "cost" in zeros and ones, and we want the maximum number of items (strings) we can pick without exceeding m zeros and n ones. Each string is either taken or not (0/1 choice), so it maps to 0/1 knapsack with two capacity dimensions. A dynamic programming approach fits: dp[i][j] = maximum number of strings using at most i zeros and j ones. Brute-force subsets is exponential and infeasible for up to 600 strings. |
| 5 | + |
| 6 | +## Refining the problem, round 2 thoughts |
| 7 | +We can compute the zeros and ones count for each string first. For DP we can either use a 3D DP over index and capacities, or a 2D DP updated per string (like typical space-optimized knapsack). To avoid reusing the same string multiple times, iterate capacities in reverse (from m downwards and n downwards). Edge cases: strings that individually require more zeros or ones than capacity — those won't be used. Time complexity will be O(L * m * n) where L = len(strs), which is fine given constraints (L ≤ 600, m,n ≤ 100). Space can be O(m * n) using the optimized 2D DP. |
| 8 | + |
| 9 | +## Attempted solution(s) |
| 10 | +```python |
| 11 | +from typing import List |
| 12 | + |
| 13 | +class Solution: |
| 14 | + def findMaxForm(self, strs: List[str], m: int, n: int) -> int: |
| 15 | + # dp[i][j] = max number of strings using at most i zeros and j ones |
| 16 | + dp = [[0] * (n + 1) for _ in range(m + 1)] |
| 17 | + |
| 18 | + for s in strs: |
| 19 | + zeros = s.count('0') |
| 20 | + ones = s.count('1') |
| 21 | + # If a string individually exceeds capacity, skip updates for impossible states. |
| 22 | + # Iterate in reverse to ensure each string is used at most once (0/1 knapsack). |
| 23 | + if zeros > m or ones > n: |
| 24 | + # Still okay to skip; it can't fit in any capacity. |
| 25 | + continue |
| 26 | + for i in range(m, zeros - 1, -1): |
| 27 | + for j in range(n, ones - 1, -1): |
| 28 | + dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1) |
| 29 | + |
| 30 | + return dp[m][n] |
| 31 | +``` |
| 32 | +- Notes: |
| 33 | + - Approach: 2D dynamic programming (space-optimized 0/1 knapsack across two dimensions). |
| 34 | + - Time complexity: O(L * m * n), where L = len(strs). For each string we update up to (m+1)*(n+1) states. |
| 35 | + - Space complexity: O(m * n) for the dp table. |
| 36 | + - Implementation detail: iterate capacities in reverse to prevent reusing the same string multiple times. Strings that require more zeros or ones than available are skipped. |
0 commit comments