Problème
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.
Contraintes
1 ≤ n ≤ 10¹⁸
Exemples
Entrée :
n = 352
Sortie attendue :
expected = 2
Entrée :
long long n = 352;
Sortie attendue :
long long expected = 2;
Entrée :
long n = 352;
Sortie attendue :
long expected = 2;
Explication
The digits are 3, 5 and 2; the smallest is 2.
Entrée :
n = 1203
Sortie attendue :
expected = 0
Entrée :
long long n = 1203;
Sortie attendue :
long long expected = 0;
Entrée :
long n = 1203;
Sortie attendue :
long expected = 0;
Explication
The 0 inside the number is the smallest digit.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Compare every decimal digit and keep the smallest one seen so far.
Indice 2
Zeros matter here, so do not ignore them while scanning the digits.
Indice 3
Initialize carefully so the first digit always updates the running minimum.
Complexité attendue
O(d) time, O(1) extra space