Problema
Look at every decimal digit of n and return the smallest one. Zeros inside the number count.
Implement smallest_digit(n).
Input
n: A positive integer.
Output
Return the smallest digit, from 0 to 9.
Restrições
1 ≤ n ≤ 10¹⁸
Exemplos
Entrada:
n = 352
Saída esperada:
expected = 2
Entrada:
long long n = 352;
Saída esperada:
long long expected = 2;
Entrada:
long n = 352;
Saída esperada:
long expected = 2;
Explicação
The digits are 3, 5 and 2; the smallest is 2.
Entrada:
n = 1203
Saída esperada:
expected = 0
Entrada:
long long n = 1203;
Saída esperada:
long long expected = 0;
Entrada:
long n = 1203;
Saída esperada:
long expected = 0;
Explicação
The 0 inside the number is the smallest digit.
Como abordar
Tente sozinho primeiro e depois abra uma dica de cada vez.
Dica 1
Compare every decimal digit and keep the smallest one seen so far.
Dica 2
Zeros matter here, so do not ignore them while scanning the digits.
Dica 3
Initialize carefully so the first digit always updates the running minimum.
Complexidade esperada
O(d) time, O(1) extra space