Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Min and Max Together

Find the smallest and largest value in one pass.

Đề bài

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].

Ràng buộc

1 ≤ length ≤ 100,000

Ví dụ

Ví dụ 1
Đầu vào:
numbers = [4, -2, 9, 0]

Kết quả mong đợi:
expected = [-2, 9]
Đầu vào:
std::vector<long long> numbers = {4, -2, 9, 0};

Kết quả mong đợi:
std::vector<long long> expected = {-2, 9};
Đầu vào:
long[] numbers = new long[] {4, -2, 9, 0};

Kết quả mong đợi:
long[] expected = new long[] {-2, 9};

Giải thích

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

Ví dụ 2
Đầu vào:
numbers = [7]

Kết quả mong đợi:
expected = [7, 7]
Đầu vào:
std::vector<long long> numbers = {7};

Kết quả mong đợi:
std::vector<long long> expected = {7, 7};
Đầu vào:
long[] numbers = new long[] {7};

Kết quả mong đợi:
long[] expected = new long[] {7, 7};

Giải thích

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.