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.
- Jesse and Cookies
- Discussions
Jesse and Cookies
Jesse and Cookies
+ 0 comments in example when they have A=[16,8,7,6] and then remove 6 and 7, the new array is [20,16,8,7] It should be [20,16,8]
every iteration decreases array's length by 1
+ 0 comments Why is my code in Python 3 not correct?
def cookies(k, A): A.sort() c = 0 while (A[0] < k): if len(A) == 1: return -1 c += 1 v = A[0] + 2*A[1] if v >= k: A.append(v) A.pop(0) else: A[1] = v A.pop(0) return c
+ 0 comments I realied that, when giving an example solution the writer counts 7 twice.
+ 0 comments JavaScript implementing MinHeap:
MinHeap class from GeeksforGeeks.
class MinHeap { constructor() { this.arr = []; } left(i) { return 2 * i + 1; } right(i) { return 2 * i + 2; } parent(i) { return Math.floor((i - 1) / 2); } size() { return this.arr.length; } getMin() { return this.arr[0]; } insert(k) { let arr = this.arr; arr.push(k); // Fix the min heap property if it is violated, HeapifyUp let i = arr.length - 1; // While newly pushed value is smaller than its parent node: while (i > 0 && arr[this.parent(i)] > arr[i]) { // Swap the new node and its parent node, then update i tobe its parent's index let p = this.parent(i); [arr[i], arr[p]] = [arr[p], arr[i]]; i = p; } } extractMin() { let arr = this.arr; if (arr.length == 1) { return arr.pop(); } // Store the minimum value, and remove it from heap let res = arr[0]; arr[0] = arr[arr.length-1]; arr.pop(); // Reheapify the arr, HeapifyDown this.MinHeapify(0); return res; } // A recursive method to heapify a subtree with the root at given index // This method assumes that the subtrees are already heapified MinHeapify(i) { let arr = this.arr; let n = arr.length; // Base case: if (n === 1) { return; } let l = this.left(i); let r = this.right(i); let smallest = i; // Find the smallest item if (l < n && arr[l] < arr[i]) { smallest = l; } if (r < n && arr[r] < arr[smallest]) { smallest = r; } // If this node is not the smallest, swap it with the smallest, then HeapifyDown again if (smallest !== i) { [arr[i], arr[smallest]] = [arr[smallest], arr[i]] this.MinHeapify(smallest); } } } function cookies(k, A) { // Write your code here const heap = new MinHeap(); A.forEach((item) => { heap.insert(item); }); let count = 0; while(heap.size() > 1 && heap.getMin() < k) { const leastValue = heap.extractMin(); const secondLeastValue = heap.extractMin(); const newValue = leastValue + (secondLeastValue * 2); heap.insert(newValue); count++; } if (heap.size() === 1 && heap.getMin() < k) { return -1; } return count; // let a = A; // let count = 0; // while (a.some(value => value < k)) { // if (a.length < 2) return -1; // const sortedA = a.sort((a, b) => a - b); // const newValue = sortedA[0] + (sortedA[1] * 2); // a.splice(0, 2, newValue); // count++; // } // return count; }
+ 0 comments JESSE, WE NEED TO COOK!
Load more conversations
Sort 116 Discussions, By:
Please Login in order to post a comment