-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path2566.maximum-difference-by-remapping-a-digit.cpp
More file actions
64 lines (61 loc) · 1.96 KB
/
2566.maximum-difference-by-remapping-a-digit.cpp
File metadata and controls
64 lines (61 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Tag: Math, Greedy
// Time: O(1)
// Space: O(1)
// Ref: -
// Note: -
// Video: https://youtu.be/pnX4dBWwczQ
// You are given an integer num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit.
// Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in num.
// Notes:
//
// When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences of d1 in num with d2.
// Bob can remap a digit to itself, in which case num does not change.
// Bob can remap different digits for obtaining minimum and maximum values respectively.
// The resulting number after remapping can contain leading zeroes.
//
//
// Example 1:
//
// Input: num = 11891
// Output: 99009
// Explanation:
// To achieve the maximum value, Bob can remap the digit 1 to the digit 9 to yield 99899.
// To achieve the minimum value, Bob can remap the digit 1 to the digit 0, yielding 890.
// The difference between these two numbers is 99009.
//
// Example 2:
//
// Input: num = 90
// Output: 99
// Explanation:
// The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0).
// Thus, we return 99.
//
// Constraints:
//
// 1 <= num <= 108
//
//
class Solution {
public:
int minMaxDifference(int num) {
int min_v = num;
int max_v = num;
for (int i = 0; i < 10; i++) {
int high = 0;
int low = 0;
int mul = 1;
int n = num;
while (n > 0) {
bool replace = n % 10 == i;
low += (replace ? 0 : n % 10) * mul;
high += (replace ? 9 : n % 10) * mul;
n /= 10;
mul *= 10;
}
min_v = min(min_v, low);
max_v = max(max_v, high);
}
return max_v - min_v;
}
};