LeetCode 1526, Hard. Topics: Array, Dynamic Programming, Stack, Greedy, Monotonic Stack. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:
Problem
You are given an integer array <code>target</code>. You have an integer array <code>initial</code> of the same size as <code>target</code> with all elements initially zeros.
In one operation you can choose <strong>any</strong> subarray from <code>initial</code> and increment each value by one.
Return <em>the minimum number of operations to form a </em><code>target</code><em> array from </em><code>initial</code>.
The test cases are generated so that the answer fits in a 32-bit integer.
Examples
Constraints
- 1 <= target.length <= 10^5
- 1 <= target[i] <= 10^5
- The input is generated such that the answer fits inside a 32 bit integer.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026