Find the minimum total added to make an array sorted by adding values to suffixes.
Unlock detailed company stats for this questionUpgrade
Given an array, make it sorted in non-decreasing order using operations that add a value to a suffix of the array, and return the minimum total amount added. Example: [3,4,1,6,2] becomes [3,4,4,9,5] after adding 3 to the suffix from index 2, then [3,4,4,9,9] after adding 4 to the last index, so the answer is 7. Example 2: [3,2,1] gives 2 (it becomes 3,3,2 then 3,3,3 after adding 1 twice).
Brute force uses two loops; the optimal approach traverses once and adds each drop (previous minus current) to a counter.
Community answers
No answers contributed by the community yet.
Related questions
Find the minimum total increment to make an array non-decreasing using suffix increases.Find the minimum total cost to make an array non-decreasing.Write pseudocode to sort an array and find the 2nd minimum, handling duplicate values.Related courses

Course

Course
Course
