[LeetCode] Make Array Non-decreasing

3523. Make Array Non-decreasing

You are given an integer array nums. In one operation, you can select a subarray and replace it with a single element equal to its maximum value.

Return the maximum possible size of the array after performing zero or more operations such that the resulting array is non-decreasing.

A subarray is a contiguous non-empty sequence of elements within an array.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int maximumPossibleSize(vector<int>& nums) {
int res = 0, ma = 0;
for(auto& n : nums) {
if(n >= ma) ma = n, res++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/04/20/PS/LeetCode/make-array-non-decreasing/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.