Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)



在计算机界中,我们总是追求用有限的资源获取最大的收益。

现在,假设你分别支配着 m 个 0 和 n 个 1。另外,还有一个仅包含 0 和 1 字符串的数组。

你的任务是使用给定的 m 个 0 和 n 个 1 ,找到能拼出存在于数组中的字符串的最大数量。每个 0 和 1 至多被使用一次。

注意:

  1. 给定 0 和 1 的数量都不会超过 100
  2. 给定字符串数组的长度不会超过 600

示例 1:

输入: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
输出: 4

解释: 总共 4 个字符串可以通过 5 个 0 和 3 个 1 拼出,即 "10","0001","1","0" 。

示例 2:

输入: Array = {"10", "0", "1"}, m = 1, n = 1
输出: 2

解释: 你可以拼出 "10",但之后就没有剩余数字了。更好的选择是拼出 "0" 和 "1" 。

dp[i][j]表示有 i个0 和 j个1时的最大数量。状态转移方程:dp[i][j] = Max(dp[i][j],dp[i-零的数量][j-一的数量]+1);
class Solution {
    public int findMaxForm(String[] strs, int m, int n) {
        int len = strs.length;
        if(len == 0) return 0;

        int[][] dp = new int[m+1][n+1];
        for (String str : strs) {
            int num0 = getZero(str);
            int num1 = str.length() - num0;

            for (int i = m; i >= num0; i--) {
                for (int j = n; j >= num1; j--) {
                    dp[i][j] = Math.max(dp[i][j],dp[i-num0][j-num1]+1);
                }
            }
        }
        return dp[m][n];
    }
    public int getZero(String str){
        int res = 0;
        for (int i = 0; i < str.length(); i++) {
            if(str.charAt(i)==‘0‘){
                res++;
            }
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/qinyuguan/p/11495013.html

时间: 2025-01-17 22:47:11

Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)的相关文章

Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在考虑网格中有障碍物.那么从左上角到右下角将会有多少条不同的路径? 网格中的障碍物和空位置分别用 1 和 0 来表示. 说明:m 和 n 的值均不超过 100. 示例 1: 输入: [   [0,0,0],  

插头DP专题

建议入门的人先看cd琦的<基于连通性状态压缩的动态规划问题>.事半功倍. 插头DP其实是比较久以前听说的一个东西,当初是水了几道水题,最近打算温习一下,顺便看下能否入门之类. 插头DP建议先理解“插头”的概念.然后会HASH表(这个其实是很基础的东西,应该都会的).然后就是DP. 以及特殊题目的特殊处理. 好像一般是求N,M<=12的网格图的某种回路数或某种通路数的方案数. 大体上每个题说几句特殊处理,有问题请纠正....题目的顺序基本上难度递增 另外代码我都是用括号匹配的.因为感觉连通

Fibonacci斐波拉契数列----------动态规划DP

n==10 20 30 40 50 46 体验一下,感受一下,运行时间 #include <stdio.h>int fib(int n){ if (n<=1)     return 1; else            return fib(n-1)+fib(n-2); }int main( ){ int n; scanf("%d",&n); printf("%d\n" ,fib(n) );} 先 n==10 20 30 40 50 46

动态规划(DP),类似LIS,FatMouse&#39;s Speed

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1108 解题报告: 1.首先按照weight从小到大排列,weight相同的按照speed从大到小排列; 2.Count[i]表示到第i个老鼠时,所求的最长“速度递减”子序列的长度: 3.path[i]=j是题目的关键,记录在Count[i]=Count[j]时,即最长“速度递减”子序列最后一个老鼠的前一只老鼠的位置 4.递归输出id void output(in

(RQoj 15 采药------rwkj 10.1.5.253 1447) 动态规划 DP 1

#include <iostream>#include <string.h>using namespace std;int dp[105][1005], w[105],v[105],T,M;int max(int x,int y){ return x>y?x:y; } void f( ){ int i,j; for (i=1; i<=M; i++) for (j=0;j<=T; j++) { if (i==0) dp[i][j]=0; else dp[i][j]=

(RQoj 15 采药------rwkj 10.1.5.253 1447) 动态规划 DP 2

70 371 10069 11 2 #include <iostream>#include <string.h>using namespace std;int dp[105][1005], w[105],v[105],T,M;int max(int x,int y){ return x>y?x:y; }void f( ){ int i,j; for (i=M; i>=1; i--) for (j=0;j<=T; j++) { if (i==M+1) dp[i][j

(RQoj 15 采药------rwkj 10.1.5.253 1447) 动态规划 DP 3

#include <iostream>#include <string.h>using namespace std;int dp[1005], w[105],v[105],T,M;int max(int x,int y) { return x>y?x:y; }void f( ){ int i,j; for (i=1; i<=M; i++) for (j=T;j>=0; j--) if (j>=w[i]) dp[j]=max(dp[j],dp[j-w[i]]+

DP专题

DP专题 1. 背包模型 2. 子序列模型 3. 递推DP 4. 区间DP 5. 树形DP 6. 状压DP 学习资料:位操作基础篇之位操作全面总结 如何快速取得一个二进制状态的所有子状态 7. 概率DP 学习资料:简说期望类问题的解法 等等.......

[dp专题]hdu 1160 FatMouse&#39;s Speed

FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10172    Accepted Submission(s): 4521Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster i