Replace Elements with Greatest Element on Right Side
给一个数组, 返回一个数组, 是原数组的每个位后边最大的值. 这个题就是longest increase subarray的变种.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Solution { public int[] replaceElements(int[] arr) { int max = -1; int temp = Integer.MIN_VALUE; for(int i = arr.length - 1; i >= 0; i--) { max = Math.max(max, temp); temp = arr[i]; arr[i] = max; } return arr; } } |