Легко Решено: 0 Процент решений: 0%

Smallest Digit

Find the smallest decimal digit of a positive integer.

Чему вы научитесь

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

Условие

Показать перевод

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.

Ограничения

1 ≤ n ≤ 10¹⁸

Примеры

Пример 1
Ввод:
n = 352

Ожидаемый вывод:
expected = 2
Ввод:
long long n = 352;

Ожидаемый вывод:
long long expected = 2;
Ввод:
long n = 352;

Ожидаемый вывод:
long expected = 2;

Пояснение

The digits are 3, 5 and 2; the smallest is 2.

Пример 2
Ввод:
n = 1203

Ожидаемый вывод:
expected = 0
Ввод:
long long n = 1203;

Ожидаемый вывод:
long long expected = 0;
Ввод:
long n = 1203;

Ожидаемый вывод:
long expected = 0;

Пояснение

The 0 inside the number is the smallest digit.

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

Compare every decimal digit and keep the smallest one seen so far.

Подсказка 2

Zeros matter here, so do not ignore them while scanning the digits.

Подсказка 3

Initialize carefully so the first digit always updates the running minimum.

Ожидаемая сложность

O(d) time, O(1) extra space

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

Принятых решений пока нет. Решите задачу и займите первое место.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.