ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Smallest of Three

Return the smallest of three integers.

สิ่งที่คุณจะได้เรียนรู้

  • Keep track of the best value so far
  • Compare values with if statements
  • Handle ties and negative numbers

โจทย์

ดูฉบับแปล

Three friends compare how many minutes they were late this week and the one who was late the least gets to pick lunch. Given the three numbers, find the smallest. Some values can be equal, and they can be negative (early).

Implement smallest_of_three(a, b, c).

Input

a: The first number.

b: The second number.

c: The third number.

Output

Return the smallest of the three values. If several are equal and smallest, return that value.

เงื่อนไข

−10^18 ≤ a, b, c ≤ 10^18

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
a = 7
b = 15
c = 3

ผลลัพธ์ที่คาดหวัง:
expected = 3
อินพุต:
long long a = 7;
long long b = 15;
long long c = 3;

ผลลัพธ์ที่คาดหวัง:
long long expected = 3;
อินพุต:
long a = 7;
long b = 15;
long c = 3;

ผลลัพธ์ที่คาดหวัง:
long expected = 3;

คำอธิบาย

3 is less than both 7 and 15.

ตัวอย่างที่ 2
อินพุต:
a = -4
b = -4
c = 10

ผลลัพธ์ที่คาดหวัง:
expected = -4
อินพุต:
long long a = -4;
long long b = -4;
long long c = 10;

ผลลัพธ์ที่คาดหวัง:
long long expected = -4;
อินพุต:
long a = -4;
long b = -4;
long c = 10;

ผลลัพธ์ที่คาดหวัง:
long expected = -4;

คำอธิบาย

The two −4 values tie for smallest, so the answer is −4.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Assume the first number is the smallest.

คำใบ้ 2

Compare it with the second and then the third, updating when you find a smaller one.

คำใบ้ 3

Use a 64-bit integer type, the values can be very large.

ความซับซ้อนที่คาดหวัง

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

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

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

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

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

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

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