Python Array effiency -


i'm working on small problem coderfights: problem description

note: if array [1,1,1,2,3] false, since has duplicate.

my program working fully, on test array 5,000 entries doesn't fit 4second window finish.

my code:

def almostincreasingsequence(s): l = len(s);  r1 = [] #result after 1st test r2 = [] #result after 2nd test t2 = [] #all true, remove false positives         #test 1 one 1 n in range(0,l):     1 = 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)     n in range(0,secondtest):         #print("running false positive test array # ",n)         duplicates = []         duplicates = t2[n]         if (len(duplicates) != len(set(duplicates))):            # print("duplicate found in",t2[n])             #if found add f r2             r2.append("f")         else:            # print("no duplicate found in, true ",t2[n])             r2.append("t")         #tf.append("f") if "t" in r2:     return true else:     return false 

what did is: first loop removed 1 element, checks if true cases. if true saves array run 2nd test on it. when loop finishes, if there arrays passed true, second test eliminates false positives checking if of them have duplicate numbers. if false positive, if no true.

finally array after second test e.g.[t,f,f] if contains t 1 of arrays not false positive.

my question how can improve performance in approach? tried not writing "f" array if false, still doesnt increase performance pass 5.000 array in less 4 seconds.

the effective approach improving performance use better algorithm. try [this pseudo-code, you'll have write actual python yourself]

# if sequence not strictly increasing there point @  #   a[n] >= a[n+1].  we'll call reversal. scan array reversal if find reversal,    #  @ point have option of removing element n,    #   or removing element n+1. try them 1 @ time:   if  a[n-1] < a[n+1] check results of removing element n      call method checks strict ordering rest         of array. [i.e. n+1 .. array.size]       if method returns true, return true                  else fall thru next check  # if here, removing element n won't work:    if a[n] < a[n+2] check results of removing element n+1        call method checks strict ordering rest           of array. [i.e. n+2 .. array.size]        return whatever value returns    else removing element n+1 won't work either      return false  # if here, didn't find reversals. return true 

note there no need remove element since problem determine whether could remove if wanted to. treating input immutable both improves performance , eliminates chances of introducing bug doing "trial removal."

you have code avoid boundary conditions @ ends of array.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -