< Summary

Information
Class: LeetCode.Algorithms.LargestTimeForGivenDigits.LargestTimeForGivenDigitsBruteForce
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LargestTimeForGivenDigits\LargestTimeForGivenDigitsBruteForce.cs
Line coverage
96%
Covered lines: 53
Uncovered lines: 2
Coverable lines: 55
Total lines: 101
Line coverage: 96.3%
Branch coverage
95%
Covered branches: 23
Total branches: 24
Branch coverage: 95.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LargestTimeFromDigits(...)95.83%242494.87%
BuildTimeString(...)100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LargestTimeForGivenDigits\LargestTimeForGivenDigitsBruteForce.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.LargestTimeForGivenDigits;
 13
 14/// <inheritdoc />
 15public sealed class LargestTimeForGivenDigitsBruteForce : ILargestTimeForGivenDigits
 16{
 17    /// <summary>
 18    ///     Time complexity - O(1)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="arr"></param>
 22    /// <returns></returns>
 23    public string LargestTimeFromDigits(int[] arr)
 224    {
 225        var maxMinutes = -1;
 26
 2027        for (var i = 0; i < 4; i++)
 828        {
 829            var h1 = arr[i];
 30
 831            if (h1 > 2)
 632            {
 633                continue;
 34            }
 35
 2036            for (var j = 0; j < 4; j++)
 837            {
 838                if (i == j)
 239                {
 240                    continue;
 41                }
 42
 643                var h2 = arr[j];
 44
 645                if (h1 == 2 && h2 > 3)
 146                {
 147                    continue;
 48                }
 49
 5050                for (var k = 0; k < 4; k++)
 2051                {
 2052                    if (i == k || j == k)
 1053                    {
 1054                        continue;
 55                    }
 56
 1057                    var m1 = arr[k];
 58
 1059                    if (m1 > 5)
 060                    {
 061                        continue;
 62                    }
 63
 1064                    var l = 6 - i - j - k;
 65
 1066                    var hours = (h1 * 10) + h2;
 1067                    var minutes = (m1 * 10) + arr[l];
 68
 1069                    var totalMinutes = (hours * 60) + minutes;
 70
 1071                    if (totalMinutes > maxMinutes)
 1072                    {
 1073                        maxMinutes = totalMinutes;
 1074                    }
 1075                }
 576            }
 277        }
 78
 279        return maxMinutes < 0 ? string.Empty : BuildTimeString(maxMinutes);
 280    }
 81
 82    private static string BuildTimeString(int totalMinutes)
 183    {
 184        var hours = totalMinutes / 60;
 185        var minutes = totalMinutes % 60;
 86
 187        var h1 = hours / 10;
 188        var h2 = hours % 10;
 189        var m1 = minutes / 10;
 190        var m2 = minutes % 10;
 91
 192        return string.Create(5, (h1, h2, m1, m2), static (dest, s) =>
 193        {
 194            dest[0] = (char)('0' + s.h1);
 195            dest[1] = (char)('0' + s.h2);
 196            dest[2] = ':';
 197            dest[3] = (char)('0' + s.m1);
 198            dest[4] = (char)('0' + s.m2);
 299        });
 1100    }
 101}