34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
'''
|
|
A young girl named Emily is learning how to subtract one from a number, but she has a unique way of doing it when the number has two or more digits.
|
|
|
|
Emily follows these steps:
|
|
If the last digit of the number is not zero, she simply subtracts one from the number.
|
|
If the last digit of the number is zero, she divides the number by 10 (effectively removing the last digit).
|
|
You are given an integer number n. Emily will perform this subtraction process k times. Your task is to determine the final result after all k subtractions. It is guaranteed that the result will always be a positive integer.
|
|
|
|
Example 1
|
|
Input:
|
|
n = 512, k = 4
|
|
Output:
|
|
50
|
|
Explanation:
|
|
512→511→510→51→50
|
|
'''
|
|
|
|
class Solution(object):
|
|
def solve(self, n:int, k:int) -> int:
|
|
for _ in range(k):
|
|
if n % 10 != 0:
|
|
n -= 1
|
|
else:
|
|
n //= 10
|
|
return n
|
|
|
|
|
|
# Example usage:
|
|
if __name__ == "__main__":
|
|
solution = Solution()
|
|
print(solution.solve(512, 4)) # Output: 50
|
|
print(solution.solve(100, 3)) # Output: 10
|
|
print(solution.solve(12345, 5)) # Output: 12340
|
|
print(solution.solve(10, 1)) # Output: 1 |