簡単 0件解決済み 正答率0%

Array Minimum

Find the smallest value in a list.

このチャレンジで学べること

  • Find the smallest value in one pass
  • Compare each element with the current best
  • Avoid a wrong starting value

問題

翻訳を表示

Return the smallest value in the list by checking each element once.

Implement array_minimum(numbers).

Input

numbers: A non-empty list of integers.

Output

Return the minimum.

制約

1 ≤ length ≤ 100,000

例

例 1
入力:
numbers = [3, 9, 2]

期待される出力:
expected = 2
入力:
std::vector<long long> numbers = {3, 9, 2};

期待される出力:
long long expected = 2;
入力:
long[] numbers = new long[] {3, 9, 2};

期待される出力:
long expected = 2;

解説

2 is smaller than 3 and 9.

例 2
入力:
numbers = [0, -5, 5]

期待される出力:
expected = -5
入力:
std::vector<long long> numbers = {0, -5, 5};

期待される出力:
long long expected = -5;
入力:
long[] numbers = new long[] {0, -5, 5};

期待される出力:
long expected = -5;

解説

-5 is the smallest.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Track the smallest value seen while scanning the list once.

ヒント 2

Compare each element to your current best and update only when smaller.

ヒント 3

Remember the list is non-empty, so you can start from the first element.

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

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