< Summary

Information
Class: LeetCode.Algorithms.OnesAndZeroes.OnesAndZeroesDynamicProgramming
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\OnesAndZeroes\OnesAndZeroesDynamicProgramming.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindMaxForm(...)100%1010100%
GetIndex(...)100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\OnesAndZeroes\OnesAndZeroesDynamicProgramming.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.OnesAndZeroes;
 13
 14/// <inheritdoc />
 15public sealed class OnesAndZeroesDynamicProgramming : IOnesAndZeroes
 16{
 17    /// <summary>
 18    ///     Time complexity - O(l * m * n), where l is the length of strs
 19    ///     Space complexity - O(m * n)
 20    /// </summary>
 21    /// <param name="strs"></param>
 22    /// <param name="m"></param>
 23    /// <param name="n"></param>
 24    /// <returns></returns>
 25    public int FindMaxForm(string[] strs, int m, int n)
 226    {
 227        Span<int> dp = stackalloc int[(m + 1) * (n + 1)];
 28
 2229        foreach (var str in strs)
 830        {
 831            var zeros = 0;
 832            var ones = 0;
 33
 6034            foreach (var c in str)
 1835            {
 1836                if (c == '0')
 937                {
 938                    zeros++;
 939                }
 40                else
 941                {
 942                    ones++;
 943                }
 1844            }
 45
 7046            for (var i = m; i >= zeros; i--)
 2747            {
 18848                for (var j = n; j >= ones; j--)
 6749                {
 6750                    var index = GetIndex(i, j, n);
 6751                    var previousIndex = GetIndex(i - zeros, j - ones, n);
 52
 6753                    dp[index] = Math.Max(dp[index], dp[previousIndex] + 1);
 6754                }
 2755            }
 856        }
 57
 258        return dp[GetIndex(m, n, n)];
 259    }
 60
 61    private static int GetIndex(int i, int j, int n)
 13662    {
 13663        return (i * (n + 1)) + j;
 13664    }
 65}