Alle Herausforderungen
Einfach 0 gelöst 0% Erfolgsquote

Make an Array Nondecreasing

Find the fewest increments needed so each value is at least the previous one.

One move increases one array value by 1. Make the array nondecreasing: each value must be greater than or equal to the one immediately before it. You may not decrease values or reorder them.

Implement increasing_array(numbers).

Input

numbers: The values in their original order.

Output

Return the minimum total number of unit increments.

Einschränkungen

1 ≤ length ≤ 200,000. Each value is between 1 and 1,000,000,000. Use a signed 64-bit result.

Beispiele

Beispiel 1
Eingabe:
numbers = [4, 1, 3]

Erwartete Ausgabe:
expected = 4
Eingabe:
std::vector<long long> numbers = {4, 1, 3};

Erwartete Ausgabe:
long long expected = 4;
Eingabe:
long[] numbers = new long[] {4, 1, 3};

Erwartete Ausgabe:
long expected = 4;

Erläuterung

Raise 1 to 4 using three moves and 3 to 4 using one move. The result [4, 4, 4] uses four moves.

Beispiel 2
Eingabe:
numbers = [1, 2, 2, 5]

Erwartete Ausgabe:
expected = 0
Eingabe:
std::vector<long long> numbers = {1, 2, 2, 5};

Erwartete Ausgabe:
long long expected = 0;
Eingabe:
long[] numbers = new long[] {1, 2, 2, 5};

Erwartete Ausgabe:
long expected = 0;

Erläuterung

Every value is already at least the preceding value; equal neighbors are allowed.

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.