Facile 0 résolus 0 % de réussite

Array Minimum

Find the smallest value in a list.

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

1 ≤ length ≤ 100,000

Exemples

Exemple 1
Entrée :
numbers = [3, 9, 2]

Sortie attendue :
expected = 2
Entrée :
std::vector<long long> numbers = {3, 9, 2};

Sortie attendue :
long long expected = 2;
Entrée :
long[] numbers = new long[] {3, 9, 2};

Sortie attendue :
long expected = 2;

Explication

2 is smaller than 3 and 9.

Exemple 2
Entrée :
numbers = [0, -5, 5]

Sortie attendue :
expected = -5
Entrée :
std::vector<long long> numbers = {0, -5, 5};

Sortie attendue :
long long expected = -5;
Entrée :
long[] numbers = new long[] {0, -5, 5};

Sortie attendue :
long expected = -5;

Explication

-5 is the smallest.

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Track the smallest value seen while scanning the list once.

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.