Tutte le sfide
Media 0 risolte 0% di accettazione

Nearest Smaller Value to the Left

For each number, find the closest earlier position containing a strictly smaller number.

Problema

For each number, find the closest earlier position containing a strictly smaller number. Equal numbers do not qualify. Positions start at 1; use 0 when there is no smaller number to the left.

Implement nearest_smaller_value(numbers).

Input

numbers: Numbers in their original order.

Output

Return one earlier position (or 0) for every input value.

Vincoli

0…200,000 integers, each between −1,000,000,000 and 1,000,000,000. Empty input returns an empty list.

Esempi

Esempio 1
Input:
numbers = [4, 2, 5, 3]

Output atteso:
expected = [0, 0, 2, 2]
Input:
std::vector<long long> numbers = {4, 2, 5, 3};

Output atteso:
std::vector<long long> expected = {0, 0, 2, 2};
Input:
long[] numbers = new long[] {4, 2, 5, 3};

Output atteso:
long[] expected = new long[] {0, 0, 2, 2};

Spiegazione

4 has no earlier value and 2 has no smaller earlier value. Both 5 and 3 find the value 2 at position 2.

Esempio 2
Input:
numbers = [2, 2, 2]

Output atteso:
expected = [0, 0, 0]
Input:
std::vector<long long> numbers = {2, 2, 2};

Output atteso:
std::vector<long long> expected = {0, 0, 0};
Input:
long[] numbers = new long[] {2, 2, 2};

Output atteso:
long[] expected = new long[] {0, 0, 0};

Spiegazione

Earlier equal values are not strictly smaller. Every answer is 0.

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.