Skip to content

Commit 1ba2cca

Browse files
committed
Maximum 69 Number
1 parent b76d354 commit 1ba2cca

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

1323-maximum-69-number.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/maximum-69-number/
3+
4+
Given a positive integer num consisting only of digits 6 and 9.
5+
Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).
6+
7+
Example 1:
8+
Input: num = 9669
9+
Output: 9969
10+
Explanation:
11+
Changing the first digit results in 6669.
12+
Changing the second digit results in 9969.
13+
Changing the third digit results in 9699.
14+
Changing the fourth digit results in 9666.
15+
The maximum number is 9969.
16+
17+
Example 2:
18+
Input: num = 9996
19+
Output: 9999
20+
Explanation: Changing the last digit 6 to 9 results in the maximum number.
21+
22+
Example 3:
23+
Input: num = 9999
24+
Output: 9999
25+
Explanation: It is better not to apply any change.
26+
27+
Constraints:
28+
1 <= num <= 10^4
29+
num's digits are 6 or 9.
30+
"""
31+
class Solution:
32+
def maximum69Number (self, num: int) -> int:
33+
six_index, i, temp = -1, 0, num
34+
while temp > 0:
35+
if temp % 10 == 6:
36+
six_index = i
37+
temp //= 10
38+
i += 1
39+
return (num + 3 *(10**six_index)) if six_index != -1 else num

0 commit comments

Comments
 (0)