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