आसान 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

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।