< Summary

Information
Class: LeetCode.Algorithms.CombinationSum2.CombinationSum2Backtracking
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CombinationSum2\CombinationSum2Backtracking.cs
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 69
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CombinationSum2(...)100%11100%
Backtrack(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CombinationSum2\CombinationSum2Backtracking.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 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.CombinationSum2;
 13
 14/// <inheritdoc />
 15public class CombinationSum2Backtracking : ICombinationSum2
 16{
 17    /// <summary>
 18    ///     Time complexity - O(2^n * k)
 19    ///     Space complexity - O(2^n * k)
 20    /// </summary>
 21    /// <param name="candidates"></param>
 22    /// <param name="target"></param>
 23    /// <returns></returns>
 24    public IList<IList<int>> CombinationSum2(int[] candidates, int target)
 225    {
 226        Array.Sort(candidates);
 27
 228        var result = new List<IList<int>>();
 29
 230        Backtrack(result, new List<int>(), candidates, target, 0);
 31
 232        return result;
 233    }
 34
 35    private static void Backtrack(ICollection<IList<int>> result, IList<int> tempList, IReadOnlyList<int> candidates,
 36        int remain, int start)
 2437    {
 2438        switch (remain)
 39        {
 40            case 0:
 641                result.Add(new List<int>(tempList));
 42
 643                break;
 44            case > 0:
 1845                {
 9446                    for (var i = start; i < candidates.Count; i++)
 4647                    {
 4648                        if (i > start && candidates[i] == candidates[i - 1])
 749                        {
 750                            continue;
 51                        }
 52
 3953                        if (candidates[i] > remain)
 1754                        {
 1755                            break;
 56                        }
 57
 2258                        tempList.Add(candidates[i]);
 59
 2260                        Backtrack(result, tempList, candidates, remain - candidates[i], i + 1);
 61
 2262                        tempList.RemoveAt(tempList.Count - 1);
 2263                    }
 64
 1865                    break;
 66                }
 67        }
 2468    }
 69}