返回最大升序数组的索引



这里,我当前返回最大升序数组的长度。但是,我想修改它,这样我就可以返回最大升序数组的索引。我尝试在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

我希望这就是您想要的。

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

最新更新