< Summary

Information
Class: LeetCode.Algorithms.MaxDifferenceYouCanGetFromChangingAnInteger.MaxDifferenceYouCanGetFromChangingAnIntegerGreedy
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaxDifferenceYouCanGetFromChangingAnInteger\MaxDifferenceYouCanGetFromChangingAnIntegerGreedy.cs
Line coverage
100%
Covered lines: 41
Uncovered lines: 0
Coverable lines: 41
Total lines: 80
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaxDiff(...)100%2020100%
ReplaceDigit(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaxDifferenceYouCanGetFromChangingAnInteger\MaxDifferenceYouCanGetFromChangingAnIntegerGreedy.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.MaxDifferenceYouCanGetFromChangingAnInteger;
 13
 14/// <inheritdoc />
 15public class MaxDifferenceYouCanGetFromChangingAnIntegerGreedy : IMaxDifferenceYouCanGetFromChangingAnInteger
 16{
 17    /// <summary>
 18    ///     Time complexity - O(log n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="num"></param>
 22    /// <returns></returns>
 23    public int MaxDiff(int num)
 424    {
 425        var mostSignificantPlace = 1;
 26
 1527        while (num / mostSignificantPlace >= 10)
 1128        {
 1129            mostSignificantPlace *= 10;
 1130        }
 31
 432        var targetForMax = -1;
 433        var targetForMin = -1;
 434        var replacementForMin = 0;
 35
 436        var firstDigit = num / mostSignificantPlace % 10;
 37
 438        if (firstDigit > 1)
 239        {
 240            targetForMin = firstDigit;
 41
 242            replacementForMin = 1;
 243        }
 44
 3845        for (var place = mostSignificantPlace; place > 0; place /= 10)
 1546        {
 1547            var digit = num / place % 10;
 48
 1549            if (targetForMax == -1 && digit < 9)
 350            {
 351                targetForMax = digit;
 352            }
 53
 1554            if (targetForMin == -1 && firstDigit <= 1 && place < mostSignificantPlace && digit > 1)
 155            {
 156                targetForMin = digit;
 57
 158                replacementForMin = 0;
 159            }
 1560        }
 61
 462        var maxValue = 0;
 463        var minValue = 0;
 64
 3865        for (var place = mostSignificantPlace; place > 0; place /= 10)
 1566        {
 1567            var digit = num / place % 10;
 68
 1569            maxValue = (maxValue * 10) + ReplaceDigit(digit, targetForMax, 9);
 1570            minValue = (minValue * 10) + ReplaceDigit(digit, targetForMin, replacementForMin);
 1571        }
 72
 473        return maxValue - minValue;
 474    }
 75
 76    private static int ReplaceDigit(int digit, int target, int replacement)
 3077    {
 3078        return digit == target ? replacement : digit;
 3079    }
 80}