< Summary

Information
Class: LeetCode.Algorithms.FindThePunishmentNumberOfInteger.FindThePunishmentNumberOfIntegerRecursive
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindThePunishmentNumberOfInteger\FindThePunishmentNumberOfIntegerRecursive.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 67
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
PunishmentNumber(...)100%44100%
CanPartitionToSum(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindThePunishmentNumberOfInteger\FindThePunishmentNumberOfIntegerRecursive.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.FindThePunishmentNumberOfInteger;
 13
 14/// <inheritdoc />
 15public class FindThePunishmentNumberOfIntegerRecursive : IFindThePunishmentNumberOfInteger
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * 2^(log10 n))
 19    ///     Space complexity - O(log10 n)
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <returns></returns>
 23    public int PunishmentNumber(int n)
 224    {
 225        var punishmentNumber = 0;
 26
 9827        for (var i = 1; i <= n; i++)
 4728        {
 4729            var square = i * i;
 4730            var squareStr = square.ToString();
 31
 4732            if (CanPartitionToSum(squareStr, 0, i, 0))
 733            {
 734                punishmentNumber += square;
 735            }
 4736        }
 37
 238        return punishmentNumber;
 239    }
 40
 41    private static bool CanPartitionToSum(string s, int index, int target, int currentSum)
 19042    {
 19043        if (index == s.Length)
 4944        {
 4945            return currentSum == target;
 46        }
 47
 14148        var num = 0;
 49
 54250        for (var i = index; i < s.Length; i++)
 22951        {
 22952            num = (num * 10) + (s[i] - '0');
 53
 22954            if (currentSum + num > target)
 8655            {
 8656                break;
 57            }
 58
 14359            if (CanPartitionToSum(s, i + 1, target, currentSum + num))
 1360            {
 1361                return true;
 62            }
 13063        }
 64
 12865        return false;
 19066    }
 67}