Fácil 0 resueltos 0% de aceptación

Array Minimum

Find the smallest value in a list.

Lo que aprenderás

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

Problema

Ver traducción

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.

Restricciones

1 ≤ length ≤ 100,000

Ejemplos

Ejemplo 1
Entrada:
numbers = [3, 9, 2]

Salida esperada:
expected = 2
Entrada:
std::vector<long long> numbers = {3, 9, 2};

Salida esperada:
long long expected = 2;
Entrada:
long[] numbers = new long[] {3, 9, 2};

Salida esperada:
long expected = 2;

Explicación

2 is smaller than 3 and 9.

Ejemplo 2
Entrada:
numbers = [0, -5, 5]

Salida esperada:
expected = -5
Entrada:
std::vector<long long> numbers = {0, -5, 5};

Salida esperada:
long long expected = -5;
Entrada:
long[] numbers = new long[] {0, -5, 5};

Salida esperada:
long expected = -5;

Explicación

-5 is the smallest.

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

Track the smallest value seen while scanning the list once.

Pista 2

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

Pista 3

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

Complejidad esperada

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

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.