簡単 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用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。