↓Skip to main content
  1. LeetCode/

LeetCode 1526: Minimum Number of Increments on Subarrays to Form a Target Array

·1 min· ·
LeetCode Hard Dynamic-Programming Greedy Monotonic-Stack Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-07-04

  • Submissions: Accepted (first try)

Approach
#

Think of target as a row of heights, where each operation raises a contiguous stretch by 1.

Scanning left to right:

  • The first cell needs target[0] operations.
  • If target[i] <= target[i - 1], cell i can reuse operations covering the previous cell (extend them), needing nothing new.
  • If target[i] > target[i - 1], the extra target[i] - target[i - 1] layers need new operations, since the previous cell is only that tall.

So the answer is target[0] + Σ max(0, target[i] - target[i - 1]).

My Solution
#

class Solution:
    def minNumberOperations(self, target: List[int]) -> int:
        ops = target[0]
        for i in range(1, len(target)):
            ops += max(0, target[i] - target[i - 1])   # rises need new operations
        return ops

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Drops add nothing: earlier operations simply stop there.

Interview Explanation
#

Treat the array as heights, with each operation raising a contiguous stretch by one layer. Going left to right, any part higher than the previous cell needs new operations, while equal or lower cells reuse earlier ones. So the answer is the first height plus every rise. Time O(n).

Related

LeetCode 1537: Get the Maximum Score
·3 mins
LeetCode Hard Dynamic-Programming Two-Pointers Greedy Array
LeetCode 1537 note: two sorted arrays can switch only at shared values; merge with two pointers, keep a running sum per path, and at a shared value set both to the larger sum plus that value
LeetCode 1793: Maximum Score of a Good Subarray
·3 mins
LeetCode Hard Two-Pointers Greedy Binary-Search Array Monotonic-Stack
LeetCode 1793 note: expand from k toward the larger neighbor each step so the minimum stays as large as possible for each length; at a boundary, only the other side can grow
LeetCode 3638: Maximum Balanced Shipments
·2 mins
LeetCode Medium Dynamic-Programming Greedy Monotonic-Stack Array
LeetCode 3638 note: greedily cut a segment as soon as a value below its maximum appears; until then the segment is non-decreasing, so its maximum is the previous value
LeetCode 84: Largest Rectangle in Histogram
·3 mins
LeetCode Daily Hard Array Stack Monotonic-Stack Dynamic-Programming Geometry Histogram
Solving the Largest Rectangle in Histogram problem using monotonic stack approach
LeetCode 1147: Longest Chunked Palindrome Decomposition
·2 mins
LeetCode Hard Dynamic-Programming Greedy Two-Pointers Rolling-Hash String
LeetCode 1147 note: build strings inward from both ends and cut a pair as soon as they match; cutting early is best, and any leftover middle counts as one chunk
LeetCode 3651: Minimum Cost Path with Teleportations
·2 mins
LeetCode Hard Dynamic-Programming Grid-Dp Matrix Array
LeetCode 3651 note: compute layer by layer over teleports; arriving at a cell by teleport costs the previous layer’s minimum over cells with value >= it, found with a running minimum after sorting, followed by the usual right / down grid DP