Python数组效率

Python数组效率,python,arrays,performance,Python,Arrays,Performance,我正在解决coderfights的一个小问题: 注意:如果数组是[1,1,1,2,3],那么它也是False,因为它有重复项 我的程序工作正常,但在5000个条目的测试阵列上,它不适合在4秒窗口中完成 我的代码: def almostIncreasingSequence(s): l = len(s); R1 = [] #RESULT AFTER 1st test R2 = [] #RESULT AFTER 2nd test T2 = [] #All true, to remove false

我正在解决coderfights的一个小问题:

注意:如果数组是[1,1,1,2,3],那么它也是False,因为它有重复项

我的程序工作正常,但在5000个条目的测试阵列上,它不适合在4秒窗口中完成

我的代码:

def almostIncreasingSequence(s):
l = len(s); 
R1 = [] #RESULT AFTER 1st test
R2 = [] #RESULT AFTER 2nd test
T2 = [] #All true, to remove false positives

       #TEST ONE 1 BY 1
for n in range(0,l):
    one = s[:];
    one.pop(n)        
    k = one[:];
    if sorted(k)==k:
        R1.append("T")
        T2.append(k)
    #else:
        R1.append("F")

        #TEST 2, REMOVE FALSE POSITIVE DUPLICATES
if "T" in R1:
   # print("will go throught",T2)

    secondTEST = len(T2)
    for n in range(0,secondTEST):
        #print("Running False Positive test for array # ",n)
        duplicates = []
        duplicates = T2[n]
        if (len(duplicates) != len(set(duplicates))):
           # print("DUPLICATE FOUND IN",T2[n])
            #if found add F to R2
            R2.append("F")
        else:
           # print("no duplicate found in, so TRUE ",T2[n])
            R2.append("T")
        #tf.append("F")
if "T" in R2:
    return True
else:
    return False
我所做的是: 第一个循环删除了1个元素,检查它是否适用于所有情况。如果为True,则保存阵列以对其运行第二次测试。循环完成后,若有数组作为True通过,则第二个测试通过检查其中是否有重复的数字来消除误报。如果他们做了假阳性,如果没有,那就是真的

最后,我在第二次测试后得到数组,例如[T,F,F],如果它包含T,那么其中一个数组不是假阳性


我的问题是如何提高我的方法的性能?如果为false,我尝试不在数组中写入“F”,但在4秒内通过5000个数组仍然无法提高性能。

提高性能的最有效方法是使用更好的算法。尝试这样的方法[这是伪代码,您必须自己编写实际的Python]

# If a sequence is not strictly increasing then there will be a point at 
#   which a[n] >= a[n+1].  We'll call that a reversal.
Scan the array for a reversal
If you find a reversal, 
  #  At this point you have an option of removing element n, 
  #   or removing element n+1. Try them one at a time:
  if  a[n-1] < a[n+1] then check the results of removing element n
     call another method that checks for strict ordering for the rest 
       of the array. [i.e. n+1 .. array.size] 
     If that method returns true, then return true
                 else fall thru to next check

# if you get here, removing element n won't work:
   if a[n] < a[n+2] then check the results of removing element n+1
       call the method that checks for strict ordering for the rest 
         of the array. [i.e. n+2 .. array.size]
       return whatever value it returns
   else removing element n+1 won't work either
     return false

# if you get here, you didn't find any reversals.
return true
#如果序列不是严格递增的,则在
#其中a[n]>=a[n+1]。我们称之为逆转。
扫描阵列以查找反转
如果你发现一个逆转,
#此时您可以选择删除元素n,
#或删除元素n+1。一次尝试一个:
如果a[n-1]
请注意,实际上不需要删除元素,因为问题只是确定是否可以删除它。将输入视为不可变既可以提高性能,又可以通过“尝试删除”消除引入错误的机会


您必须非常小心地编写代码,以避免数组末端的边界条件。

提高性能的最有效方法是使用更好的算法。尝试这样的方法[这是伪代码,您必须自己编写实际的Python]

# If a sequence is not strictly increasing then there will be a point at 
#   which a[n] >= a[n+1].  We'll call that a reversal.
Scan the array for a reversal
If you find a reversal, 
  #  At this point you have an option of removing element n, 
  #   or removing element n+1. Try them one at a time:
  if  a[n-1] < a[n+1] then check the results of removing element n
     call another method that checks for strict ordering for the rest 
       of the array. [i.e. n+1 .. array.size] 
     If that method returns true, then return true
                 else fall thru to next check

# if you get here, removing element n won't work:
   if a[n] < a[n+2] then check the results of removing element n+1
       call the method that checks for strict ordering for the rest 
         of the array. [i.e. n+2 .. array.size]
       return whatever value it returns
   else removing element n+1 won't work either
     return false

# if you get here, you didn't find any reversals.
return true
#如果序列不是严格递增的,则在
#其中a[n]>=a[n+1]。我们称之为逆转。
扫描阵列以查找反转
如果你发现一个逆转,
#此时您可以选择删除元素n,
#或删除元素n+1。一次尝试一个:
如果a[n-1]
请注意,实际上不需要删除元素,因为问题只是确定是否可以删除它。将输入视为不可变既可以提高性能,又可以通过“尝试删除”消除引入错误的机会


您必须非常小心地编写代码,以避免数组末端出现边界条件。

您能解释一下为什么
[1,1,2,3]
的计算结果为False吗?若你们移除其中一个1,那个么它的顺序是严格的。现在编辑,我在数组中遗漏了一个额外的1。谢谢你的关注!你能解释一下为什么
[1,1,2,3]
的计算结果为False吗?若你们移除其中一个1,那个么它的顺序是严格的。现在编辑,我在数组中遗漏了一个额外的1。谢谢你的关注!