mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
35 lines
1.2 KiB
HTML
35 lines
1.2 KiB
HTML
|
<p>Given an array <code>arr</code>, replace every element in that array with the greatest element among the elements to its right, and replace the last element with <code>-1</code>.</p>
|
||
|
|
||
|
<p>After doing so, return the array.</p>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Example 1:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> arr = [17,18,5,4,6,1]
|
||
|
<strong>Output:</strong> [18,6,6,6,1,-1]
|
||
|
<strong>Explanation:</strong>
|
||
|
- index 0 --> the greatest element to the right of index 0 is index 1 (18).
|
||
|
- index 1 --> the greatest element to the right of index 1 is index 4 (6).
|
||
|
- index 2 --> the greatest element to the right of index 2 is index 4 (6).
|
||
|
- index 3 --> the greatest element to the right of index 3 is index 4 (6).
|
||
|
- index 4 --> the greatest element to the right of index 4 is index 5 (1).
|
||
|
- index 5 --> there are no elements to the right of index 5, so we put -1.
|
||
|
</pre>
|
||
|
|
||
|
<p><strong>Example 2:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> arr = [400]
|
||
|
<strong>Output:</strong> [-1]
|
||
|
<strong>Explanation:</strong> There are no elements to the right of index 0.
|
||
|
</pre>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Constraints:</strong></p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>1 <= arr.length <= 10<sup>4</sup></code></li>
|
||
|
<li><code>1 <= arr[i] <= 10<sup>5</sup></code></li>
|
||
|
</ul>
|