LeetCode Problem Workspace

Make Array Non-decreasing

Determine the maximum size of a non-decreasing array by replacing subarrays with their maximum values efficiently.

category

4

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Medium · Stack-based state management

bolt

Answer-first summary

Determine the maximum size of a non-decreasing array by replacing subarrays with their maximum values efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Stack-based state management

Try AiBox Copilotarrow_forward

Use a stack-based state management to iterate through the array backwards, merging subarrays into their maximum values. Track transitions where elements decrease to decide optimal merges. This approach ensures the resulting array remains non-decreasing while maximizing its length in a single pass.

Problem Statement

You are given an integer array nums. You can perform an operation where a subarray is replaced with a single element equal to its maximum value. The goal is to maximize the resulting array's size while keeping it non-decreasing.

Return the maximum possible size of the array after performing zero or more operations. For example, given nums = [4,2,5,3,5], one optimal sequence results in [4,5,5] with size 3.

Examples

Example 1

Input: nums = [4,2,5,3,5]

Output: 3

One way to achieve the maximum size is: The final array [4, 5, 5] is non-decreasing with size 3.

Example 2

Input: nums = [1,2,3]

Output: 3

No operation is needed as the array [1,2,3] is already non-decreasing.

Constraints

  • 1 <= nums.length <= 2 * 105
  • 1 <= nums[i] <= 2 * 105

Solution Approach

Iterate Backwards with Stack

Process the array from the end to the start, using a stack to maintain groups of values that can be merged. Push and pop stack elements based on whether the current element is less than or equal to the top of the stack.

Merge Decreasing Sequences

Whenever a current element is smaller than the top of the stack, merge it with the stack's top by replacing the subarray with its maximum. This ensures non-decreasing order and allows counting merged segments.

Count Maximum Array Size

After processing all elements, the size of the stack represents the maximum number of elements in the non-decreasing array. Each stack element corresponds to a final value segment in the array.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n) as each element is pushed and popped at most once. Space complexity is O(n) for the stack in the worst case when no merges occur.

What Interviewers Usually Probe

  • Do you notice the decreasing sequences in nums and how to manage them?
  • Have you considered iterating backwards to simplify merge decisions with a stack?
  • Can you explain how each operation affects the final array size and order?

Common Pitfalls or Variants

Common pitfalls

  • Merging forward instead of backward may require complex lookahead logic.
  • Forgetting to update the maximum when merging subarrays can yield incorrect counts.
  • Ignoring equal elements can reduce the array size unnecessarily.

Follow-up variants

  • Count minimum operations to make the array non-decreasing instead of maximum size.
  • Apply the same method to multidimensional arrays along one axis using stack-based state tracking.
  • Restrict merges to subarrays of fixed length and compute maximum non-decreasing size.

FAQ

What does 'Make Array Non-decreasing' mean in this context?

It means modifying the array with allowed subarray replacement operations so that no element is smaller than the previous one.

Why should I iterate backwards in this problem?

Backward iteration simplifies tracking which subarrays to merge with a stack, avoiding complex forward lookahead.

Can we merge non-consecutive elements?

No, only consecutive subarrays can be merged, which is why a stack accurately tracks potential merges.

What stack pattern is used here?

This problem uses a monotonic stack pattern to maintain non-decreasing order while counting merges efficiently.

How do I calculate the maximum size after operations?

After processing all elements with the stack and merging where necessary, the stack size represents the maximum array length.

terminal

Solution

Solution 1

#### Python3

1
2
3
4
5
6
7
8
class Solution:
    def maximumPossibleSize(self, nums: List[int]) -> int:
        ans = mx = 0
        for x in nums:
            if mx <= x:
                ans += 1
                mx = x
        return ans
Make Array Non-decreasing Solution: Stack-based state management | LeetCode #3523 Medium