Skip to content

Commit 44ba784

Browse files
chore: add LeetCode daily solution
1 parent 43874bb commit 44ba784

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Ones and Zeroes (Medium)
2+
3+
**Problem ID:** 474
4+
**Date:** 2025-11-11
5+
**Link:** https://leetcode.com/problems/ones-and-zeroes/
6+
7+
## Approach
8+
9+
To solve the "Ones and Zeroes" problem, we can utilize a dynamic programming approach similar to the "0/1 Knapsack" problem. The main idea is to maintain a 2D DP table that keeps track of the maximum size of the subset of binary strings that can be formed with a given number of zeros and ones.
10+
11+
### Approach:
12+
13+
1. **Count Zeros and Ones**: For each binary string in the input array, count the number of zeros and ones. This can be done using simple iteration over each string.
14+
15+
2. **Dynamic Programming Table**: Create a 2D DP array `dp` where `dp[i][j]` represents the maximum size of the subset that can be formed with at most `i` zeros and `j` ones.
16+
17+
3. **Initialization**: Initialize the DP table with zeros. The base case is `dp[0][0] = 0`, meaning with zero zeros and zero ones, the maximum subset size is zero.
18+
19+
4. **Filling the DP Table**:
20+
- Iterate over each string and its corresponding count of zeros (`zero_count`) and ones (`one_count`).
21+
- Update the DP table in reverse order (from `m` to `zero_count` and from `n` to `one_count`). This prevents overwriting results of the current iteration.
22+
- For each valid combination of zeros and ones, update the DP value as follows:
23+
```
24+
dp[i][j] = max(dp[i][j], dp[i - zero_count][j - one_count] + 1)
25+
```
26+
- This means we can either choose to include the current string (hence the `+1`) or not include it.
27+
28+
5. **Result Extraction**: The result will be found at `dp[m][n]`, which gives the size of the largest subset that can be formed with at most `m` zeros and `n` ones.
29+
30+
### Data Structures:
31+
- A 2D list (or array) for the DP table, where dimensions are `(m + 1) x (n + 1)`.
32+
33+
### Complexity:
34+
- **Time Complexity**: O(S * m * n), where `S` is the number of strings in `strs`. This is due to iterating through each string and updating the DP table.
35+
- **Space Complexity**: O(m * n) for the DP table.
36+
37+
This approach efficiently computes the maximum subset size while adhering to the constraints on the number of zeros and ones, leveraging the principles of dynamic programming to build up solutions from smaller subproblems.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int findMaxForm(String[] strs, int m, int n) {
3+
int[][] dp = new int[m + 1][n + 1];
4+
5+
for (String str : strs) {
6+
int zeros = 0, ones = 0;
7+
for (char c : str.toCharArray()) {
8+
if (c == '0') zeros++;
9+
else ones++;
10+
}
11+
12+
for (int i = m; i >= zeros; i--) {
13+
for (int j = n; j >= ones; j--) {
14+
dp[i][j] = Math.max(dp[i][j], dp[i - zeros][j - ones] + 1);
15+
}
16+
}
17+
}
18+
19+
return dp[m][n];
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var findMaxForm = function(strs, m, n) {
2+
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
3+
4+
for (const str of strs) {
5+
const count0 = (str.match(/0/g) || []).length;
6+
const count1 = str.length - count0;
7+
8+
for (let i = m; i >= count0; i--) {
9+
for (let j = n; j >= count1; j--) {
10+
dp[i][j] = Math.max(dp[i][j], dp[i - count0][j - count1] + 1);
11+
}
12+
}
13+
}
14+
15+
return dp[m][n];
16+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
3+
dp = [[0] * (n + 1) for _ in range(m + 1)]
4+
5+
for s in strs:
6+
zeros = s.count('0')
7+
ones = s.count('1')
8+
for i in range(m, zeros - 1, -1):
9+
for j in range(n, ones - 1, -1):
10+
dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1)
11+
12+
return dp[m][n]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
275275
- 2025-11-08 — [Minimum One Bit Operations to Make Integers Zero](https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/) (Hard) → `Hard/2025-11-08-1611-Minimum-One-Bit-Operations-to-Make-Integers-Zero`
276276
- 2025-11-09 — [Count Operations to Obtain Zero](https://leetcode.com/problems/count-operations-to-obtain-zero/) (Easy) → `Easy/2025-11-09-2169-Count-Operations-to-Obtain-Zero`
277277
- 2025-11-10 — [Minimum Operations to Convert All Elements to Zero](https://leetcode.com/problems/minimum-operations-to-convert-all-elements-to-zero/) (Medium) → `Medium/2025-11-10-3542-Minimum-Operations-to-Convert-All-Elements-to-Zero`
278+
- 2025-11-11 — [Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/) (Medium) → `Medium/2025-11-11-474-Ones-and-Zeroes`

0 commit comments

Comments
 (0)