Problema
Return the largest value in the list by checking each element once.
Implement array_maximum(numbers).
Input
numbers: A non-empty list of integers.
Output
Return the maximum.
Restricciones
1 ≤ length ≤ 100,000
Ejemplos
Entrada:
numbers = [3, 9, 2]
Salida esperada:
expected = 9
Entrada:
std::vector<long long> numbers = {3, 9, 2};
Salida esperada:
long long expected = 9;
Entrada:
long[] numbers = new long[] {3, 9, 2};
Salida esperada:
long expected = 9;
Explicación
9 is larger than 3 and 2.
Entrada:
numbers = [-4, -1, -7]
Salida esperada:
expected = -1
Entrada:
std::vector<long long> numbers = {-4, -1, -7};
Salida esperada:
long long expected = -1;
Entrada:
long[] numbers = new long[] {-4, -1, -7};
Salida esperada:
long expected = -1;
Explicación
With only negative values, -1 is the largest.
Cómo abordarlo
Inténtalo primero por tu cuenta y luego abre las pistas una a una.
Pista 1
Start with the first element as the best value so far.
Pista 2
Scan the list once and update the best value whenever you find a larger one.
Pista 3
Do not assume values are positive; negatives can still be the maximum.
Complejidad esperada
O(n) time, O(1) extra space
Referencias
Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.