Skip to main content
All Questions

Find the minimum total cost to make an array non-decreasing.

Unlock detailed company stats for this questionUpgrade

Given an integer array nums of length n, in one operation you choose a contiguous segment nums[l..r] (0 <= l <= r < n) and a positive integer x, then add x to every element in that segment at cost x. You may perform any number of operations. Return the minimum total cost to make nums non-decreasing (nums[i] <= nums[i+1] for all 0 <= i < n - 1).

Examples: [3,1,2,4] -> 2 (add 2 to segment [1..2]); [1,2,3] -> 0; [5,3,4,1] -> 5 (add 2 to [1..3], then 3 to [3..3]). Constraints: 1 <= n <= 10^5, 1 <= nums[i] <= 10^9.

Format · Online assessment DSA question.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.