We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Contest is over, so I'm allowed to post code, right? Here's my partially correct solution. I would be very grateful if someone could help me with this.
#!/bin/python3importsysdeflargestValue(n,A):dp=[0,A[0]*A[1]]rsum=[A[0]]foriinrange(1,n):rsum+=[rsum[-1]+A[i]]foriinrange(2,n):dp+=[dp[-1]+rsum[i-1]*A[i]]m=max(dp)res=mforiinrange(n):# remove if-statement for O(n^2) -> passes 100% for n <= 5000if(dp[i]!=m):continuerrsum=rsum[i]dp2=dp[i]forjinrange(0,i):rrsum-=A[j]dp2-=A[j]*rrsumres=max(dp2,res)returnresif__name__=="__main__":n=int(input().strip())A=list(map(int,input().strip().split(' ')))result=max(largestValue(n,A),largestValue(n,A[::-1]))print(result)
I find the maximum value of arrays A[0:i] for all i and just "assume" A[max_i] is an endpoint of the desired subarray. Then I just find the maximum value of arrays containing one of those endpoints.
Surprisingly, this passes for 70.4/80 points (18/21 correct). Is this because of generous test cases? Also, is A = [0,0,...,0] the only worst-case for this algorithm?
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Pair Sums
You are viewing a single comment's thread. Return to all comments →
Contest is over, so I'm allowed to post code, right? Here's my partially correct solution. I would be very grateful if someone could help me with this.
I find the maximum value of arrays A[0:i] for all i and just "assume" A[max_i] is an endpoint of the desired subarray. Then I just find the maximum value of arrays containing one of those endpoints.
Surprisingly, this passes for 70.4/80 points (18/21 correct). Is this because of generous test cases? Also, is A = [0,0,...,0] the only worst-case for this algorithm?