-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (85 loc) · 2.59 KB
/
main.go
File metadata and controls
92 lines (85 loc) · 2.59 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Source: https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer
// Title: Max Difference You Can Get From Changing an Integer
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given an integer `num`. You will apply the following steps to `num` **two** separate times:
//
// - Pick a digit `x (0 <= x <= 9)`.
// - Pick another digit `y (0 <= y <= 9)`. Note `y` can be equal to `x`.
// - Replace all the occurrences of `x` in the decimal representation of `num` by `y`.
//
// Let `a` and `b` be the two results from applying the operation to `num` independently.
//
// Return the max difference between `a` and `b`.
//
// Note that neither `a` nor `b` may have any leading zeros, and **must not** be 0.
//
// **Example 1:**
//
// ```
// Input: num = 555
// Output: 888
// Explanation: The first time pick x = 5 and y = 9 and store the new integer in a.
// The second time pick x = 5 and y = 1 and store the new integer in b.
// We have now a = 999 and b = 111 and max difference = 888
// ```
//
// **Example 2:**
//
// ```
// Input: num = 9
// Output: 8
// Explanation: The first time pick x = 9 and y = 9 and store the new integer in a.
// The second time pick x = 9 and y = 1 and store the new integer in b.
// We have now a = 9 and b = 1 and max difference = 8
// ```
//
// **Constraints:**
//
// - `1 <= num <= 10^8`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"strconv"
"strings"
)
// To maximize the number, remap to hightest non-9 digit to 9.
// To minimize the number,
// * if the hightest digit is not 1, remap all that digit to 1
// * otherwise, remap th highest non-0 & non-1 digit to 0
func maxDiff(num int) int {
digits := strconv.Itoa(num)
// Maximize
maxDigit := '9'
for _, digit := range digits {
if digit != '9' {
maxDigit = digit
break
}
}
maxDigits := digits
if maxDigit != '9' {
maxDigits = strings.ReplaceAll(digits, string(maxDigit), "9")
}
maxNum, _ := strconv.Atoi(maxDigits)
// Minimize
minDigits := digits
if digits[0] != '1' {
minDigits = strings.ReplaceAll(digits, string(digits[0]), "1")
} else {
minDigit := '0'
for _, digit := range digits {
if digit != '0' && digit != '1' {
minDigit = digit
break
}
}
if minDigit != '0' && minDigit != '1' {
minDigits = strings.ReplaceAll(digits, string(minDigit), "0")
}
}
minNum, _ := strconv.Atoi(minDigits)
return maxNum - minNum
}