-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadd-digits.py
68 lines (59 loc) · 1.77 KB
/
add-digits.py
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
# 258. Add Digits
# 🟢 Easy
#
# https://leetcode.com/problems/add-digits/
#
# Tags: Math - Simulation - Number Theory
import timeit
# The straightforward solution is to recursively or iteratively compute
# the sum of some digits, for example the last two, until the value only
# has one digit.
#
# Time complexity: O(log(n)) - In each iteration, we divide by 100.
# Space complexity: O(1) - We use constant extra memory.
#
# Runtime 34 ms Beats 62.21%
# Memory 13.8 MB Beats 39.79%
class Iterative:
def addDigits(self, num: int) -> int:
while num > 9:
num = num // 10 + num % 10
return num
# Using math, we can compute the digital root of a value using the mod
# of the value and the base-1.
#
# Time complexity: O(1) - Constant time.
# Space complexity: O(1) - We use constant extra memory.
#
# Runtime 56 ms Beats 6.65%
# Memory 16.3 MB Beats 39.79%
class Math:
def addDigits(self, num: int) -> int:
return 1 + (num - 1) % 9 if num else 0
def test():
executors = [
Iterative,
Math,
]
tests = [
[0, 0],
[38, 2],
[2147483647, 1],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.addDigits(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()