Условие
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
Примеры
Ввод:
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.
Ввод:
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
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. Следуйте условию и ограничениям выше.