当前位置:网站首页>Dynamic programming - 474. One and zero
Dynamic programming - 474. One and zero
2022-07-28 03:45:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Here's an array of binary strings strs And two integers m and n .
Please find out and return to strs Length of the largest subset of , This subset most Yes m individual 0 and n individual 1 .
If x All of the elements of are also y The elements of , aggregate x Is a collection y Of A subset of .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/ones-and-zeroes
2 Title Example
Example 1:
Input :strs = [“10”, “0001”, “111001”, “1”, “0”], m = 5, n = 3
Output :4
explain : At most 5 individual 0 and 3 individual 1 The largest subset of is {“10”,“0001”,“1”,“0”} , So the answer is 4 .
Other smaller subsets that satisfy the question include {“0001”,“1”} and {“10”,“1”,“0”} .{“111001”} Not satisfied with the question , Because it contains 4 individual 1 , Greater than n Value 3 .
Example 2:
Input :strs = [“10”, “0”, “1”], m = 1, n = 1
Output :2
explain : The largest subset is {“0”, “1”} , So the answer is 2 .
3 Topic tips
1 <= strs.length <= 600
1 <= strs[i].length <= 100
strs[i] Only by ‘0’ and ‘1’ form
1 <= m, n <= 100
4 Ideas
This problem is very similar to the classic knapsack problem , But there is only one capacity different from the classical knapsack problem , This problem has two capacities , That is... In the selected string subset 00 and 11 The maximum number of .
The classical knapsack problem can be solved by two-dimensional dynamic programming , The two dimensions are goods and capacity . This problem has two capacities , Therefore, it is necessary to use three-dimensional dynamic programming to solve , The three dimensions are string 、00 Capacity and 11 The capacity of .
Start the dynamic rule trilogy :
- determine dp Array (dp table) And the meaning of subscripts
dp[i][j]: At most i individual 0 and j individual 1 Of strs The size of the largest subset of is dp[i][j]. - Determine the recurrence formula
dp[i][j] It can be from the previous strs Derived from the string in ,strs The string in the has zeroNum individual 0,oneNum individual 1.
dp[i][j] It could be dp[i - zeroNum][j - oneNum] + 1.
Then we go through the process of traversal , take dp[i][j] The maximum of .
So the recurrence formula :dp[i][j] = max(dp[i][j], dp[i - zeroNum][j - oneNum] + 1);
Now you can recall 01 Recursive knapsack formula :dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
A comparison will show that , A string of zeroNum and oneNum Equivalent to the weight of the article (weight[i]), The number of strings is equivalent to the value of the item (value[i]).
This is a typical 01 knapsack ! It's just that the weight of an object has two dimensions . - dp How to initialize an array
Because the value of an item is not negative , For the initial 0, Guarantee the time of recurrence dp[i][j] Not covered by initial values . - Determine the traversal order
The object is strs The string in , The capacity of the backpack is in the description of the topic m and n. - Give an example to deduce dp Array
5 My answer
class Solution {
public int findMaxForm(String[] strs, int m, int n) {
//dp[i][j] Express i individual 0 and j individual 1 Maximum subset of
int[][] dp = new int[m + 1][n + 1];
int oneNum, zeroNum;
for (String str : strs) {
oneNum = 0;
zeroNum = 0;
for (char ch : str.toCharArray()) {
if (ch == '0') {
zeroNum++;
} else {
oneNum++;
}
}
// Reverse traversal
for (int i = m; i >= zeroNum; i--) {
for (int j = n; j >= oneNum; j--) {
dp[i][j] = Math.max(dp[i][j], dp[i - zeroNum][j - oneNum] + 1);
}
}
}
return dp[m][n];
}
}
边栏推荐
- ASEMI整流桥GBPC3510在直流有刷电机中的妙用
- Play WolframAlpha computing knowledge engine
- [错题]Concatenation
- Container related concepts
- C语言力扣第45题之跳跃游戏 II。遍历跳跃
- Server memory failure prediction can actually do this!
- [P4] solve the conflict between local file modification and library file
- [openvx] VX for basic use of objects_ matrix
- Volvo: what on earth does the deep-rooted "sense of security" rely on?
- 超好用的 PC 端长截图工具
猜你喜欢

The wonderful use of asemi rectifier bridge GBPC3510 in DC brush motor

【图像分类】2021-MLP-Mixer NIPS
![[paper notes] mobile robot autonomous navigation experimental platform based on deep learning](/img/6a/7f0c2b2a53332636f3172bc3b0b74d.png)
[paper notes] mobile robot autonomous navigation experimental platform based on deep learning

Server memory failure prediction can actually do this!

In December, the PMP Exam adopted the new syllabus for the first time. How to learn?

AI首席架构师12-AICA-百度OCR垂类规模化落地实践

In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe trial

接口自动化测试,完整入门篇

Advanced Mathematics (Seventh Edition) Tongji University exercises 3-6 personal solutions

Vertical align align the elements in the row are vertically centered
随机推荐
Greed 122. The best time to buy and sell stocks II
【OPENVX】对象基本使用之vx_distribution
做自动化测试,你后悔了吗?
ES6 from getting started to mastering 09: symbol type
SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe 试读版
数据丰富的计算:M.2在边缘遇到AI
【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
MySQL Basics (create, manage, add, delete, and modify tables)
[openvx] VX for basic use of objects_ convolution
[P4] check the differences between the two historical versions of the library file
LeetCode_409_最长回文串
Prefix-Tuning: Optimizing Continuous Prompts for Generation
高等数学(第七版)同济大学 习题3-5 个人解答
Asemi rectifier bridge gbpc5010, gbpc5010 parameters, gbpc5010 size
A 404 page source code imitating win10 blue screen
Light year admin background management system template
《剑指offer》| 刷题小记
input 上传文件并回显 FileReader并限制选择文件时的类型
Jumping game II in question 45 of C language power deduction. Ergodic jump
【P4】解决本地文件修改与库文件间的冲突问题