Fácil 0 resolvidos 0% de aceitação

Smallest Digit

Find the smallest decimal digit of a positive integer.

O que você vai aprender

  • Visit every digit of a number
  • Keep the smallest value seen so far
  • Choose a good starting value for a minimum

Problema

Ver tradução

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

Exemplo 1
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.

Exemplo 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

A melhor solução aprovada de cada programador. As mais rápidas, leves e mais bem avaliadas ficam no topo.

Ainda não há soluções aprovadas. Resolva e fique com o primeiro lugar.

Aprenda com outros

Soluções da comunidade

Ainda não há soluções publicadas. Resolva este desafio para compartilhar a primeira.