Skip to content

Commit a068a6a

Browse files
Create README.md
1 parent a22cbcc commit a068a6a

File tree

1 file changed

+48
-0
lines changed
  • Medium/1432. Max Difference You Can Get From Changing an Integer

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+

0 commit comments

Comments
 (0)