0

The question is the following: Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j]. The width of such a ramp is j - i.

Find the maximum width of a ramp in A. If one doesn't exist, return 0.

class Solution(object):
def maxWidthRamp(self, A):
    ans = 0
    m = float('inf')
    for i in sorted(range(len(A)), key = A.__getitem__):
        ans = max(ans, i - m)
        m = min(m, i)
    return ans

solution is as followed: For all elements like A[i] = v, let's write the indices i in sorted order of their values v. For example with A[0] = 7, A[1] = 2, A[2] = 5, A[3] = 4, we can write the order of indices i=1, i=3, i=2, i=0.

Then, whenever we write an index i, we know there was a ramp of width i - min(indexes_previously_written) (if this quantity is positive). We can keep track of the minimum of all indexes previously written as m.

This is one of solution given by leetcode. However, I do not understand how this solution works and have no ideas what are meant by "A.getitem" and "float('inf)

  • What's the question number on Leetcode? –  Apr 29 '20 at 13:15
  • I'm not sure if you tried and read these posts but they might be helpful : https://stackoverflow.com/questions/34264710/what-is-the-point-of-floatinf-in-python and this: https://stackoverflow.com/questions/43627405/understanding-getitem-method – barshopen Apr 29 '20 at 13:21
  • You can literally put `float('inf') python` into a search engine and get relevant, helpful answers in the first few results. – Karl Knechtel Apr 29 '20 at 13:40

0 Answers0