474 Ones and Zeroes

class Solution {
    public int findMaxForm(String[] strs, int m, int n) {

        int[][] dp = new int[m + 1][n + 1];

        for (int k = 0; k < strs.length; k++){
            String cur = strs[k];
            int[] counts = count(cur);
            for (int i = m; i >= counts[0]; i--){
                for (int j = n; j >= counts[1]; j--){
                    dp[i][j] = Math.max(dp[i][j], 1 + dp[i - counts[0]][j - counts[1]]);
                }
            }
        }

        return dp[m][n];
    }

    private int[] count(String str){
        int[] counts = new int[2];
        for (int i = 0; i < str.length(); i++){
            char c = str.charAt(i);
            if (c == '0') counts[0]++;
            if (c == '1') counts[1]++;
        }

        return counts;
    }
}

results matching ""

    No results matching ""