Skip to content

Commit 2b71881

Browse files
committed
LeetCode 1323. Maximum 69 Number
1 parent b8e47c8 commit 2b71881

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
286286
| [1268. Search Suggestions System][lc1268] | 🟠 Medium | [![python](res/py.png)][lc1268py] |
287287
| [1293. Shortest Path in a Grid with Obstacles Elimination][lc1293] | 🔴 Hard | [![python](res/py.png)][lc1293py] |
288288
| [1296. Divide Array in Sets of K Consecutive Numbers][lc1296] | 🟠 Medium | [![python](res/py.png)][lc1296py] |
289+
| [1323. Maximum 69 Number][lc1323] | 🟠 Medium | [![python](res/py.png)][lc1323py] |
289290
| [1328. Break a Palindrome][lc1328] | 🟠 Medium | [![python](res/py.png)][lc1328py] |
290291
| [1329. Sort the Matrix Diagonally][lc1329] | 🟠 Medium | [![python](res/py.png)][lc1329py] |
291292
| [1332. Remove Palindromic Subsequences][lc1332] | 🟢 Easy | [![python](res/py.png)][lc1332py] |
@@ -867,6 +868,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
867868
[lc1293py]: leetcode/shortest-path-in-a-grid-with-obstacles-elimination.py
868869
[lc1296]: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
869870
[lc1296py]: leetcode/divide-array-in-sets-of-k-consecutive-numbers.py
871+
[lc1323]: https://leetcode.com/problems/maximum-69-number/
872+
[lc1323py]: leetcode/maximum-69-number.py
870873
[lc1328]: https://leetcode.com/problems/break-a-palindrome/
871874
[lc1328py]: leetcode/break-a-palindrome.py
872875
[lc1329]: https://leetcode.com/problems/sort-the-matrix-diagonally/

leetcode/maximum-69-number.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 1323. Maximum 69 Number
2+
# 🟢 Easy
3+
#
4+
# https://leetcode.com/problems/maximum-69-number/
5+
#
6+
# Tags: Math - Greedy
7+
8+
import timeit
9+
10+
11+
# We can obtain the largest number converting the 6 with the highest
12+
# order into a 9, if none are found, then it is optimal to not modify
13+
# the input.
14+
#
15+
# Time complexity: O(1) - We iterate 3 times over the input, converting
16+
# to a list of strings, finding the first 6 and casting back to int.
17+
# Since the input has a maximum of 4 digits, time is bounded.
18+
# Space complexity: O(1) - Where n is the number of digits in the input,
19+
# we create a list of digits. Since the input has a maximum of 4 digits,
20+
# space is bounded.
21+
#
22+
# Runtime: 38 ms, faster than 84.45%
23+
# Memory Usage: 13.8 MB, less than 54.69%
24+
class Parsing:
25+
def maximum69Number(self, num: int) -> int:
26+
return int(str(num).replace("6", "9", 1))
27+
28+
29+
# Use divmod inside a loop to find the index of the furthest left 6 in
30+
# the input, then add 3 at that position to update that value to a 9.
31+
#
32+
# Time complexity: O(1) - The loop will execute a maximum of 4 times
33+
# because num <= 10^4.
34+
# Space complexity: O(1) - Constant space.
35+
#
36+
# Runtime: 67 ms, faster than 10.70%
37+
# Memory Usage: 14 MB, less than 9.69%
38+
class Math:
39+
def maximum69Number(self, num: int) -> int:
40+
# The digit that we are visiting left to right.
41+
i = 0
42+
# Make a copy of num to mutate it.
43+
quotient = num
44+
# Store the furthest left index at which we find a number 6.
45+
highest_six = -1
46+
while quotient > 0:
47+
quotient, remainder = divmod(quotient, 10)
48+
if remainder == 6:
49+
highest_six = i
50+
i += 1
51+
# If we found any 6, update the highest 6 found to become a 9
52+
# by adding 3 at that position in the input number.
53+
return (num + 3 * (10**highest_six)) if highest_six != -1 else num
54+
55+
56+
def test():
57+
executors = [
58+
Parsing,
59+
Math,
60+
]
61+
tests = [
62+
[9669, 9969],
63+
[9996, 9999],
64+
[9999, 9999],
65+
]
66+
for executor in executors:
67+
start = timeit.default_timer()
68+
for _ in range(1):
69+
for col, t in enumerate(tests):
70+
sol = executor()
71+
result = sol.maximum69Number(t[0])
72+
exp = t[1]
73+
assert result == exp, (
74+
f"\033[93m» {result} <> {exp}\033[91m for"
75+
+ f" test {col} using \033[1m{executor.__name__}"
76+
)
77+
stop = timeit.default_timer()
78+
used = str(round(stop - start, 5))
79+
cols = "{0:20}{1:10}{2:10}"
80+
res = cols.format(executor.__name__, used, "seconds")
81+
print(f"\033[92m» {res}\033[0m")
82+
83+
84+
test()

0 commit comments

Comments
 (0)