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.
privatestaticintJesseAndCookies(intk,List<int>sweetness){sweetness.Sort();if(sweetness[0]>=k)return0;if(sweetness.Count>=2&&sweetness[0]+2*sweetness[1]>=k)return1;List<int>mixed=newList<int>();mixed.Add(sweetness[0]+2*sweetness[1]);intoperations=1,mixedIndex=0,sweetnessIndex=2;intmixedSum=0;boolisThereSumGreaterThanK=false;// While there are elements < k remainingwhile(sweetnessIndex<sweetness.Count&&mixedIndex<mixed.Count&&sweetness[sweetnessIndex]<k){// Last element of sweetnessif(sweetnessIndex==sweetness.Count-1){// Find the next two elements to apply the function to, if possibleif(mixedIndex>=mixed.Count)returnisThereSumGreaterThanK?operations+1:-1;mixedSum=sweetness[sweetnessIndex]<=mixed[mixedIndex]?sweetness[sweetnessIndex++]+2*mixed[mixedIndex++]:mixed[mixedIndex++]+2*sweetness[sweetnessIndex++];if(mixedSum<k){mixed.Add(mixedSum);}elseif(!isThereSumGreaterThanK){isThereSumGreaterThanK=true;}operations++;}else{// Find the next two elements to apply the function toif(mixed[mixedIndex]<=sweetness[sweetnessIndex]){mixedSum=mixed[mixedIndex++]+2*sweetness[sweetnessIndex++];}elseif(mixed[mixedIndex]<sweetness[sweetnessIndex+1]){mixedSum=sweetness[sweetnessIndex++]+2*mixed[mixedIndex++];}else{mixedSum=sweetness[sweetnessIndex]+2*sweetness[sweetnessIndex+1];sweetnessIndex+=2;}// Add it to mixed if its smaller than kif(mixedSum<k){mixed.Add(mixedSum);}// Notify that there is at least 1 element >= kelseif(!isThereSumGreaterThanK){isThereSumGreaterThanK=true;}// New operationoperations++;}}// While there are elements < k im sweetnesswhile(sweetnessIndex<sweetness.Count&&sweetness[sweetnessIndex]<k){// Last element of sweetness < k // Find the next two elements to apply the function to, if possibleif(sweetnessIndex==sweetness.Count-1){returnisThereSumGreaterThanK?operations+1:-1;}// Apply the function to the next 2 consecutive elems in sweetneesmixedSum=sweetness[sweetnessIndex++]+2*sweetness[sweetnessIndex++];if(mixedSum<k){mixed.Add(mixedSum);}elseif(!isThereSumGreaterThanK){isThereSumGreaterThanK=true;}operations++;}// While there are elements in mixedwhile(mixedIndex<mixed.Count){// Last element of mixedif(mixedIndex==mixed.Count-1){// Find the next two elements to apply the function to, if possibleif(sweetnessIndex>=sweetness.Count){returnisThereSumGreaterThanK?operations+1:-1;}operations++;mixedIndex++;}else{// Apply the function to the next 2 consecutive elems in mixedmixedSum=mixed[mixedIndex++]+2*mixed[mixedIndex++];if(mixedSum<k){mixed.Add(mixedSum);}elseif(!isThereSumGreaterThanK){isThereSumGreaterThanK=true;}operations++;}}returnoperations;}
My thinking here is that it is only necessary to consider those mixed cookies whose sweetness < k for future calculations. Cookies with sweetness >= k are only necessary to consider when only 1 element < k remains alone at the end of the calculations.
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 →
C# solution without PriorityQueue.
My thinking here is that it is only necessary to consider those mixed cookies whose sweetness < k for future calculations. Cookies with sweetness >= k are only necessary to consider when only 1 element < k remains alone at the end of the calculations.