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

Min and Max Together

Find the smallest and largest value in one pass.

โจทย์

Walk through the list once and track both the smallest and the largest value.

Implement min_and_max(numbers).

Input

numbers: A non-empty list of integers.

Output

Return [smallest, largest].

เงื่อนไข

1 ≤ length ≤ 100,000

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
numbers = [4, -2, 9, 0]

ผลลัพธ์ที่คาดหวัง:
expected = [-2, 9]
อินพุต:
std::vector<long long> numbers = {4, -2, 9, 0};

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {-2, 9};
อินพุต:
long[] numbers = new long[] {4, -2, 9, 0};

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {-2, 9};

คำอธิบาย

The smallest value is -2 and the largest is 9: [-2, 9].

ตัวอย่างที่ 2
อินพุต:
numbers = [7]

ผลลัพธ์ที่คาดหวัง:
expected = [7, 7]
อินพุต:
std::vector<long long> numbers = {7};

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {7, 7};
อินพุต:
long[] numbers = new long[] {7};

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {7, 7};

คำอธิบาย

With one value it is both the smallest and largest: [7, 7].

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

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

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

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

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