返回最大升序数组的索引

问题描述

这里,我目前返回最大升序数组的长度。但是,我想修改它,以便我可以返回最大升序数组的索引。我尝试在 if (A[i] > A[i-1]) : 中设置 idx = I 但这似乎不起作用。我还能尝试什么?

def func(A):

    n = len(A)
    m = 1
    l = 1
    idx = -1
      
    # traverse the array from the 2nd element
    for i in range(1,n) :
 
        # if current element if greater than prevIoUs
        # element,then this element helps in building
        # up the prevIoUs increasing subarray encountered
        # so far
        if (A[i] > A[i-1]) :
            l =l + 1
            idx = i-1
        else :
 
            # check if 'max' length is less than the length
            # of the current increasing subarray. If true,# then update 'max'
            if (m < l)  :
                m = l

            # reset 'len' to 1 as from this element
            # again the length of the new increasing
            # subarray is being calculated   
            l = 1
         
         
    # comparing the length of the last
    # increasing subarray with 'max'
    if (m < l) :
        m = l
      
    # required maximum length
    return m

解决方法

我希望这就是您要找的。​​p>

def func(A):
    n = len(A)
    m = 1
    l = 1
    index = [0,0]
    flag=0
    for i in range(1,n):
        if (A[i] > A[i-1]) :
            l =l + 1
            if flag==0:
                flag=1
                lindex = i-1
        else:
            if (m < l)  :
                m = l
                index = [lindex,i-1]
            flag=0
            l = 1
    if (m < l) :
        m = l
        index = [lindex,i]
        
    return index