2022-03-27 18:35:17 +08:00
< 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 >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 18:35:17 +08:00
< 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 >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 18:35:17 +08:00
< 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 >