I am submitting this code snippet of python 3.4 in problem ALTSEQ and it always gives TLE. Can anyone please help me and guide how to optimize this further?
def alternatingSeq(arr,n):
if(n==1):
return 1dp = [1 for i in range(n)]
res = 0
for i in range(1,n):
for j in range(i):
if( ((arr[i]<0 and arr[j] > 0 and -arr[i] > arr[j])
or (arr[i]>0 and arr[j] < 0 and arr[i] > -arr[j]))
and dp[i] < 1 + dp[j]):
dp[i] = 1 + dp[j]
if(dp[i] > res):
res = dp[i]
return res
n = int(input())
arr = [int(i) for i in input().split()]
print(alternatingSeq(arr,n))
created
last reply
- 2
replies
- 583
views
- 2
users
- 1
like