Dynamic programming learning exercises (1)

General learning

In fact, many people think about algorithm problems based on a kind of violent solution, try to enumerate, if the enumeration is successful, maybe the problem will be completed, but the enumeration fails, then they are helpless, when the test system tells you that the running time of the problem exceeds When the scope is limited, he is still quite bald.
In fact, dynamic programming was used to solve problems before, but I found that this algorithm is analogous to English words. If you don't use it for a long time, you will forget it. When I wanted to try the dynamic programming solution again, I found that apart from remembering the four words of the knapsack problem , my head was blank, so I went to the Great God's blog to learn about the knapsack problem of dynamic programming. Every time I finish learning this classic algorithm, I can give myself a new feeling, so I decided to go to the Likou zone to brush a few more such questions and practice it.

474. One and Zero (Li Kou)

给你一个二进制字符串数组 strs 和两个整数 m 和 n 。

请你找出并返回 strs 的最大子集的大小,该子集中 最多 有 m 个 0 和 n 个 1

如果 x 的所有元素也是 y 的元素,集合 x 是集合 y 的 子集 。

 

示例 1

输入:strs = ["10", "0001", "111001", "1", "0"], m = 5, n = 3
输出:4
解释:最多有 5031 的最大子集是 {
    
    "10","0001","1","0"} ,因此答案是 4
其他满足题意但较小的子集包括 {
    
    "0001","1"}{
    
    "10","1","0"}{
    
    "111001"} 不满足题意,因为它含 41 ,大于 n 的值 3
示例 2

输入:strs = ["10", "0", "1"], m = 1, n = 1
输出:2
解释:最大的子集是 {
    
    "0", "1"} ,所以答案是 2
 

提示:

1 <= strs.length <= 600
1 <= strs[i].length <= 100
strs[i] 仅由 '0''1' 组成
1 <= m, n <= 100

With full of enthusiasm, I want to try the algorithm that I understand to try to solve it, but this question really gave me a head. What the backpack problem teaches us is how to use the variable of the backpack volume to limit the choices. The greatest value, this question gave two constraints, emmmm...Think carefully, in fact, there are still commonalities between the problems. Although the backpack problem has only one limiting factor on the surface, he still added another when considering it dynamically. Constraints on the number of items that can be selected . Although this question has two restrictions, since each string can only be used once (ie a limited backpack), when updating dp(i, j), i and j are both Need to enumerate from largest to smallest. As for why it should be understood this way, it is equivalent to the reverse choice of the knapsack problem. We use dp(i, j) to represent the maximum number of strings that can be spelled out using i 0s and j 1s, then the state transition equation is:

dp[i][j] = Math.max(1+dp[i-arr[0]][j-arr[1]], dp[i][j]);

Where k represents the k-th string, arr[0] and arr[1] represent the number of 0 and 1 in the string


    public static int findMaxForm(String[] strs, int m, int n) {
    
    
    	int[][] dp = new int[m+1][n+1];
    	for(int k = 0;k < strs.length;k++) {
    
    
    		int[] arr = intcount(strs[k]);
        	for(int i = m;i >= arr[0];i--) {
    
    
        		for(int j = n;j >= arr[1];j--) {
    
    
        			dp[i][j] = Math.max(1+dp[i-arr[0]][j-arr[1]], dp[i][j]);
        		}
        	}
    	}
    	for(int i = 0;i <= m;i++) {
    
    
    		for(int j = 0;j <= n;j++) {
    
    
    			System.out.print(dp[i][j]+" ");
    		}
    		System.out.println();
    	}
    	return 0;
    }
    public static int[] intcount(String str) {
    
    
    	int[] arr = new int[2];
    	for(int i = 0;i < str.length();i++) {
    
    
    		if(str.charAt(i) == '0')
    			arr[0]++;
    		if(str.charAt(i) == '1')
    			arr[1]++;
    	}
    	return arr;
    }

Guess you like

Origin blog.csdn.net/baldicoot_/article/details/115188158