100. Maximum Width Ramp

MediumArrayArray

A ramp in an integer array nums is a pair (i, j) where i < j and nums[i] <= nums[j]. The width of such a ramp is j - i. Return the maximum width of a ramp in nums. If no ramp exists, return 0.

Input: Integer array nums.

Output: Integer — maximum width of any ramp, or 0.

Examples

Example 1
Input: [6,0,8,2,1,5]
Output: 4
Explanation: Ramp (1,5): nums[1]=0<=nums[5]=5. Width=4.
Example 2
Input: [9,8,1,0,1,9,4,0,4,1]
Output: 7
Explanation: Ramp (2,9): nums[2]=1<=nums[9]=1. Width=7.
Example 3
Input: [1,2,3,4,5]
Output: 4
Explanation: Ramp (0,4): 1<=5. Width=4.

Constraints

Asked by

AccentureGoogleAmazonBloombergMicrosoft
Solve this problem in the editor →