Условие
Look at every decimal digit of n and return the largest one.
Implement largest_digit(n).
Input
n: A positive integer.
Output
Return the largest digit, from 0 to 9.
Ограничения
1 ≤ n ≤ 10¹⁸
Примеры
Ввод:
n = 352
Ожидаемый вывод:
expected = 5
Ввод:
long long n = 352;
Ожидаемый вывод:
long long expected = 5;
Ввод:
long n = 352;
Ожидаемый вывод:
long expected = 5;
Пояснение
The digits are 3, 5 and 2; the largest is 5.
Ввод:
n = 7
Ожидаемый вывод:
expected = 7
Ввод:
long long n = 7;
Ожидаемый вывод:
long long expected = 7;
Ввод:
long n = 7;
Ожидаемый вывод:
long expected = 7;
Пояснение
The only digit is 7.
Как подступиться
Сначала попробуйте сами, затем открывайте подсказки по одной.
Подсказка 1
Compare every decimal digit and keep the biggest one seen so far.
Подсказка 2
Extract digits one by one from the right, or inspect the string characters.
Подсказка 3
A number containing 9 anywhere immediately sets the answer to 9.
Ожидаемая сложность
O(d) time, O(1) extra space