File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Medium/1432. Max Difference You Can Get From Changing an Integer Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 🧮 LeetCode 1432 - Max Difference You Can Get From Changing an Integer
2
+
3
+ ## 📌 Problem Statement
4
+
5
+ You are given an integer ` num ` . You can perform the following operation ** two separate times** :
6
+
7
+ 1 . Choose a digit ` x ` (0 ≤ x ≤ 9).
8
+ 2 . Choose another digit ` y ` (0 ≤ y ≤ 9). ` y ` can be equal to ` x ` .
9
+ 3 . Replace ** all** occurrences of digit ` x ` in ` num ` with digit ` y ` .
10
+
11
+ Let ` a ` and ` b ` be the two results from applying the operation to ` num ` .
12
+ Return the ** maximum possible difference** between ` a ` and ` b ` .
13
+
14
+ ### ⚠️ Conditions:
15
+ - ` a ` and ` b ` must not have leading zeros.
16
+ - ` a ` and ` b ` must not be zero.
17
+
18
+ ---
19
+
20
+ ## 💡 Example
21
+
22
+ ### Example 1:
23
+ ### Input:
24
+ ```
25
+ num = 555
26
+ ```
27
+ ### Output:
28
+ ```
29
+ 888
30
+ ```
31
+
32
+ ### Explanation:
33
+ + a = 999 (replace 5 with 9)
34
+ + b = 111 (replace 5 with 1)
35
+ + Max Difference = 999 - 111 = 888
36
+
37
+ ---
38
+ ## ✅ Approach
39
+
40
+ We aim to:
41
+ - ** Maximize the number** by replacing the first non-9 digit with ` 9 ` .
42
+ - ** Minimize the number** :
43
+ - If the first digit is not ` 1 ` , change it to ` 1 ` .
44
+ - Otherwise, look for a digit (not 0 or 1) to replace with ` 0 ` , avoiding leading zeros.
45
+
46
+ ---
47
+
48
+
You can’t perform that action at this time.
0 commit comments