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.
Thanks for your hints. I have to consider (r == 1) with my solution. How to optimize my solution so that I don't need to consider the corner case. Looking forward to your reply!
defcountTriplets(arr,r):singleMap={}doubleMap={}tripleMap={}fornuminarr:# every number in this array can be the first number of tripletssingleMap.setdefault(num,0)singleMap[num]+=1# if it can be the second number of tripletsifnum%r==0:first=num//riffirstinsingleMap:doubleMap.setdefault((first,num),0)doubleMap[(first,num)]+=singleMap[first]ifnum%r==0andnum%(r*r)==0:# it can be the third number in the tripletsfirst,second=num//r//r,num//rif(first,second)indoubleMap:tripleMap.setdefault((first,second,num),0)tripleMap[(first,second,num)]+=doubleMap[(first,second)]ifr==1:returnsum(map(lambdan:n*(n-1)*(n-2)//6,singleMap.values()))returnsum(tripleMap.values())
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Count Triplets
You are viewing a single comment's thread. Return to all comments →
Thanks for your hints. I have to consider (r == 1) with my solution. How to optimize my solution so that I don't need to consider the corner case. Looking forward to your reply!