| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.MaxDifferenceYouCanGetFromChangingAnInteger; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public 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) |
| 4 | 24 | | { |
| 4 | 25 | | var mostSignificantPlace = 1; |
| | 26 | |
|
| 15 | 27 | | while (num / mostSignificantPlace >= 10) |
| 11 | 28 | | { |
| 11 | 29 | | mostSignificantPlace *= 10; |
| 11 | 30 | | } |
| | 31 | |
|
| 4 | 32 | | var targetForMax = -1; |
| 4 | 33 | | var targetForMin = -1; |
| 4 | 34 | | var replacementForMin = 0; |
| | 35 | |
|
| 4 | 36 | | var firstDigit = num / mostSignificantPlace % 10; |
| | 37 | |
|
| 4 | 38 | | if (firstDigit > 1) |
| 2 | 39 | | { |
| 2 | 40 | | targetForMin = firstDigit; |
| | 41 | |
|
| 2 | 42 | | replacementForMin = 1; |
| 2 | 43 | | } |
| | 44 | |
|
| 38 | 45 | | for (var place = mostSignificantPlace; place > 0; place /= 10) |
| 15 | 46 | | { |
| 15 | 47 | | var digit = num / place % 10; |
| | 48 | |
|
| 15 | 49 | | if (targetForMax == -1 && digit < 9) |
| 3 | 50 | | { |
| 3 | 51 | | targetForMax = digit; |
| 3 | 52 | | } |
| | 53 | |
|
| 15 | 54 | | if (targetForMin == -1 && firstDigit <= 1 && place < mostSignificantPlace && digit > 1) |
| 1 | 55 | | { |
| 1 | 56 | | targetForMin = digit; |
| | 57 | |
|
| 1 | 58 | | replacementForMin = 0; |
| 1 | 59 | | } |
| 15 | 60 | | } |
| | 61 | |
|
| 4 | 62 | | var maxValue = 0; |
| 4 | 63 | | var minValue = 0; |
| | 64 | |
|
| 38 | 65 | | for (var place = mostSignificantPlace; place > 0; place /= 10) |
| 15 | 66 | | { |
| 15 | 67 | | var digit = num / place % 10; |
| | 68 | |
|
| 15 | 69 | | maxValue = (maxValue * 10) + ReplaceDigit(digit, targetForMax, 9); |
| 15 | 70 | | minValue = (minValue * 10) + ReplaceDigit(digit, targetForMin, replacementForMin); |
| 15 | 71 | | } |
| | 72 | |
|
| 4 | 73 | | return maxValue - minValue; |
| 4 | 74 | | } |
| | 75 | |
|
| | 76 | | private static int ReplaceDigit(int digit, int target, int replacement) |
| 30 | 77 | | { |
| 30 | 78 | | return digit == target ? replacement : digit; |
| 30 | 79 | | } |
| | 80 | | } |