Loading...
You are given an integer array nums.
For each index i, look forward for the first index j with j > i and nums[j] <= nums[i]. If such a j exists, index i contributes nums[i] - nums[j]; if no later value is less than or equal to nums[i], index i contributes nums[i] unchanged.
Note that the comparison is less than or equal to, so an equal later value counts. Each index is resolved against the original array, and the subtractions do not cascade.
Return an array result of the same length, where result[i] is the value contributed by index i.
Input: nums = [8,4,6,2,3]
Output: [4,2,4,2,3]
Explanation: Index 0 holds 8; the first later value at most 8 is the 4 at index 1, so 8 - 4 = 4. Index 1 holds 4; the first later value at most 4 is the 2 at index 3, so 4 - 2 = 2. Index 2 holds 6, resolved by the same 2, giving 6 - 2 = 4. Index 3 holds 2 and no later value is at most 2 (the 3 at index 4 is larger), so it stays 2. Index 4 has nothing after it and stays 3.
Input: nums = [1,2,3,4,5]
Output: [1,2,3,4,5]
Explanation: The array is strictly increasing, so no index has a later value less than or equal to it and every value is unchanged.
Input: nums = [10,1,1,6]
Output: [9,0,1,6]
Explanation: Index 0 is resolved by the 1 at index 1, giving 10 - 1 = 9. Index 1 holds 1 and the equal 1 at index 2 counts, giving 1 - 1 = 0. Index 2 has only the larger 6 after it, so it stays 1, and index 3 stays 6.
nums.length ≤500nums[i] ≤1000Click "Run" to test with sample cases or "Submit" to run all tests.