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.
Scala Solution (imports and I/O omitted for brevity):
objectResult{defcomputeOperations(k:Long,input:Array[Long]):Int={// Handle edge casesif(input.length<2){if(input.isEmpty||input(0)<k)return-1return0}// Store in appropriate data structure (heap)// Reverse order so min -> maxvalcookies:PriorityQueue[Long]=(PriorityQueue[Long]()++=input).reverse// Perform operations until either done or only one cookie leftvaroperations=0while(cookies.length>=2&&cookies.head<k){valnewCookie:Long=cookies.dequeue()+2L*cookies.dequeue()cookies.addOne(newCookie)operations+=1}// Evaluate final cookie if only one left; else return number of operationsif(cookies.nonEmpty&&cookies.head>=k)operationselse-1}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Jesse and Cookies
You are viewing a single comment's thread. Return to all comments →
Scala Solution (imports and I/O omitted for brevity):