Skip to content

Instantly share code, notes, and snippets.

@matharoo
matharoo / peak.js
Created October 20, 2019 20:56
In an array of ints, return the index such that the sum of the elements to the right of that index equals the sum of the elements to the left of that index
function peak(arr) {
let leftsum = 0;
let rightsum = 0;
mid = Math.floor(arr.length / 2)
const hash = {}
while (mid > 0) {
leftsum = 0;
rightsum = 0;
for (x = mid - 1; x >= 0; x--) {
leftsum += arr[x]
@matharoo
matharoo / PriorityQueue.js
Created September 23, 2019 16:28
Priority Queue implementation in Javascript
/**
* A priority queue stores a list of items but each can have a numeric priority value.
* Items with a higher priority are dequeued before items with a lower priority.
* Implemented as a hash of arrays where the hash keys are priority values.
*/
function PriorityQueue() { //removed unused size variable
this.store = {}; // keys are priorities, values are arrays of elements
this.count = 0;
// either keep all the functions inside Priority Queue or outside.
// only keep properties of object inside constructor and define the Object properties on the prototype function so that its easy to read and follow the code.