Skip to content

Commit dab376a

Browse files
Sync LeetCode submission Runtime - 3 ms (70.62%), Memory - 206.2 MB (90.11%)
1 parent f666a42 commit dab376a

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<p>You are given an integer array <code>nums</code>. In one operation, you can select a <span data-keyword="subarray-nonempty">subarray</span> and replace it with a single element equal to its <strong>maximum</strong> value.</p>
2+
3+
<p>Return the <strong>maximum possible size</strong> of the array after performing zero or more operations such that the resulting array is <strong>non-decreasing</strong>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<div class="example-block">
9+
<p><strong>Input:</strong> <span class="example-io">nums = [4,2,5,3,5]</span></p>
10+
11+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
12+
13+
<p><strong>Explanation:</strong></p>
14+
15+
<p>One way to achieve the maximum size is:</p>
16+
17+
<ol>
18+
<li>Replace subarray <code>nums[1..2] = [2, 5]</code> with <code>5</code> &rarr; <code>[4, 5, 3, 5]</code>.</li>
19+
<li>Replace subarray <code>nums[2..3] = [3, 5]</code> with <code>5</code> &rarr; <code>[4, 5, 5]</code>.</li>
20+
</ol>
21+
22+
<p>The final array <code>[4, 5, 5]</code> is non-decreasing with size <font face="monospace">3.</font></p>
23+
</div>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<div class="example-block">
28+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>
29+
30+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
31+
32+
<p><strong>Explanation:</strong></p>
33+
34+
<p>No operation is needed as the array <code>[1,2,3]</code> is already non-decreasing.</p>
35+
</div>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li>
42+
<li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>5</sup></code></li>
43+
</ul>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int maximumPossibleSize(vector<int>& nums) {
4+
int n = nums.size();
5+
int ans = 0;
6+
int mx = 0;
7+
for (int num : nums) {
8+
if (num >= mx) {
9+
mx = num;
10+
ans++;
11+
}
12+
}
13+
return ans;
14+
}
15+
};

0 commit comments

Comments
 (0)