ง่าย 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

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก