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.
After several try, I finally pass this Hard problem with the following code.
It's maybe possible to improve it in term of time complexity.
The logic is to take all product possibilities using the itertool.product() method, then, with a temp value, see if the current result is better than the output.
# Enter your code here. Read input from STDIN. Print output to STDOUTfromitertoolsimportproductK,M=map(int,input().split())all_lists=[]foriinrange(K):all_lists.append(list(map(int,input().split()[1:])))#print(all_lists)forsub_listinall_lists:sub_list.sort()#all_product_possibilities = list(product(*all_lists))# print(all_product_possibilities[:5])output=0#print("-------------------------")forpossible_productinlist(product(*all_lists)):current_product=list(possible_product)#print(f"current_product = {current_product}")temp=0forvaluesincurrent_product:temp+=values**2temp=temp%M#print(f"Temp: {temp}")#print(f"Output: {output}")iftemp>output:output=tempelse:continue#print(f"Current Output = {output}")print(output)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximize It!
You are viewing a single comment's thread. Return to all comments →
After several try, I finally pass this Hard problem with the following code. It's maybe possible to improve it in term of time complexity. The logic is to take all product possibilities using the itertool.product() method, then, with a temp value, see if the current result is better than the output.