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.
Wow, this one just doesn't want to get solved no matter what. First I tried doing it with a recursive function, well that ran into the RuntimeError. No biggie let's read up on DP. So we need to go through all A:s and add possible sums of those into a list. Nope still a runtime error. Ok how about going through numbers from 0 to G and mark those of them that can be easily summed to? Well that leads to counting A[0]+A[0] etc, so I try recording also the "route" for each of them as a list, well that's a MemoryError...
Amazingly it's the recursive function that has so far managed to solve the most amount of cases(3 - 0,7,11), while both my attempts at dynamic programming solve no more than 2 cases, before they either timeout or raise a memory/runtime error.
E.g. this code seems to net correct output if I let it run for a couple of minutes on my own computer:
T=int(input().strip())forruninrange(T):N,G=map(int,input().strip().split())A=[int(i)foriininput().strip().split()]ifmax(A)>Gorsum(A)>2*G:print('NO')else:states=[0]#sum of A:sforiinA:states.sort()forxinstates[:]:ifi+x>G:breakelifi+xnotinstates:states.append(x+i)ifsum(A)-max(states)<=G:print('YES')else:print('NO')
But on the server it just gets terminated before it finishes and I have no idea how to further improve it's performance. Any suggestions?
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Indian Job
You are viewing a single comment's thread. Return to all comments →
Wow, this one just doesn't want to get solved no matter what. First I tried doing it with a recursive function, well that ran into the RuntimeError. No biggie let's read up on DP. So we need to go through all A:s and add possible sums of those into a list. Nope still a runtime error. Ok how about going through numbers from 0 to G and mark those of them that can be easily summed to? Well that leads to counting A[0]+A[0] etc, so I try recording also the "route" for each of them as a list, well that's a MemoryError...
Amazingly it's the recursive function that has so far managed to solve the most amount of cases(3 - 0,7,11), while both my attempts at dynamic programming solve no more than 2 cases, before they either timeout or raise a memory/runtime error.
E.g. this code seems to net correct output if I let it run for a couple of minutes on my own computer:
But on the server it just gets terminated before it finishes and I have no idea how to further improve it's performance. Any suggestions?