- Prepare
- Data Structures
- Heap
- Jesse and Cookies
- Discussions
Jesse and Cookies
Jesse and Cookies
+ 1 comment Quite frustrating when one needs to use multiple languages just to beat the timer. It feels like the time constraint was not supplied adequately for each supported language.
E.g.: TS and JS timed out (with two arrays for heap and queue), then Java15 timed out (with one ArrayList), but python3 did it. The logic was the same but only the latter beat the clock thanks to the built in heapify.
+ 0 comments ez af, if u cant do it, ky5
+ 1 comment Someone correct me if im mistaken, but the example is wrong when it iterates over element 7 twice. On line where it states 'Remove 6,7, return ' after this there should be no '7' present in the exmple array A. The next line states 'Finaly, remove 8,7 ' but 7 has already been removed in the previous line. so it should read as follows; Remove 6,7, return 6+ 2 * 7 = 20 ans A= 20,16,8 Remove 8,16, return 8+ 2 * 16 = 40 ans A= [40,20] stop as values are above 9, that is still 4 iteration
+ 0 comments import heapq def cookies(k, A): h=[i for i in A] heapq.heapify(h) ans=0 while h[0]<k and len(h)>1: f=heapq.heappop(h) s=heapq.heappop(h) heapq.heappush(h,f+2*s) ans+=1 if h[0]>=k: return ans else: return -1
+ 1 comment It seems like there is actualy an error in the example. Going from the second step to the third step in the example, the array gets "8,7" removed but "7" is still included in the array after it was already removed. I'm also not sure why my code only passes some tests but not all. I tried testing edge cases but it seems like everything works fine.
function cookies(k, A) { let array = A.sort((a,b) => a-b) let counter = 0 if (array[0] >= k || array.length <= 1){ return -1 } else { while (array[0] < k){ let first = array.shift() let second = array.shift() let third = parseInt(first) + 2 * parseInt(second) array.push(third) array.sort((a,b) => a-b) counter += 1 } return counter } }
Sort 337 Discussions, By:
Please Login in order to post a comment