You are viewing a single comment's thread. Return to all comments →
JS with queue, no heap:
function cookies(k, A) { A.sort((a, b) => a - b); let count = 0; let i = 0; let cookies = A; let mixed = []; while (cookies.length > 1) { while ((i < cookies.length && cookies[i] <= k)) { let current = cookies[i]; let next = cookies[i + 1]; if (mixed.length) { if (current >= mixed[0]) { next = current; current = mixed.shift(); } else if (!(mixed[0] > next)) { next = mixed.shift(); } else { i += 1; } i += 1; } else { if (next === undefined) break; i += 2; } const mix = current + next * 2; count += 1; mixed.push(mix); } cookies = mixed; mixed = []; i = 0; } return cookies[0] < k ? -1 : count; }
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 →
JS with queue, no heap: