Math¶
Table of Contents¶
- 1945. Sum of Digits of String After Convert (Easy)
- 1894. Find the Student that Will Replace the Chalk (Medium)
- 7. Reverse Integer (Medium)
1945. Sum of Digits of String After Convert¶
# Math
def getLucky(s: str, k: int) -> int:
def getSum(n: int) -> int:
total = 0
while n != 0:
n, m = divmod(n, 10)
total += m
return total
result = ""
for i in s:
result += str(ord(i) - ord("a") + 1)
result = int(result)
for _ in range(k):
result = getSum(result)
return result
# |------------|------- |---------|
# | Approach | Time | Space |
# |------------|--------|---------|
# | Math | O(n) | O(1) |
# |------------|--------|---------|
s = "iiii"
k = 1
print(getLucky(s, k)) # 36
1894. Find the Student that Will Replace the Chalk¶
from typing import List
# Math
def chalkReplacer(chalk: List[int], k: int) -> int:
total = sum(chalk)
k %= total
for i, c in enumerate(chalk):
k -= c
if k < 0:
return i
return -1
# |------------|------- |---------|
# | Approach | Time | Space |
# |------------|--------|---------|
# | Math | O(n) | O(1) |
# |------------|--------|---------|
chalk = [5, 1, 5]
k = 22
print(chalkReplacer(chalk, k)) # 0