< Summary

Information
Class: LeetCode.Algorithms.LargestDivisibleSubset.LargestDivisibleSubsetDynamicProgramming
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LargestDivisibleSubset\LargestDivisibleSubsetDynamicProgramming.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 66
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
LargestDivisibleSubset(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LargestDivisibleSubset\LargestDivisibleSubsetDynamicProgramming.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.LargestDivisibleSubset;
 13
 14/// <inheritdoc />
 15public class LargestDivisibleSubsetDynamicProgramming : ILargestDivisibleSubset
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public IList<int> LargestDivisibleSubset(int[] nums)
 224    {
 225        Array.Sort(nums);
 26
 227        var dp = new int[nums.Length];
 228        var prev = new int[nums.Length];
 29
 230        Array.Fill(dp, 1);
 231        Array.Fill(prev, -1);
 32
 233        var maxIndex = 0;
 34
 1435        for (var i = 1; i < nums.Length; i++)
 536        {
 2837            for (var j = 0; j < i; j++)
 938            {
 939                if (nums[i] % nums[j] == 0 && dp[j] + 1 > dp[i])
 840                {
 841                    dp[i] = dp[j] + 1;
 42
 843                    prev[i] = j;
 844                }
 945            }
 46
 547            if (dp[i] > dp[maxIndex])
 448            {
 449                maxIndex = i;
 450            }
 551        }
 52
 253        var result = new List<int>();
 54
 855        while (maxIndex != -1)
 656        {
 657            result.Add(nums[maxIndex]);
 58
 659            maxIndex = prev[maxIndex];
 660        }
 61
 262        result.Reverse();
 63
 264        return result;
 265    }
 66}