Skip to content

Instantly share code, notes, and snippets.

@peterkhayes
peterkhayes / disjointSet.js
Created May 3, 2017 05:33
Javascript Disjoint Set
/*
Useful for HackerRank problems.
*/
function main (numNodes, edges) {
const parent = [];
const rank = [];
function makeSet (x) {
@peterkhayes
peterkhayes / gist:fcb7548ef7cde329c460
Last active May 6, 2016 01:33
Lecture notes for Intermediate Data Structures talk
I) About me:
a) Peter Hayes, engineer at ClassDojo
b) Studied mathematical economics at Brown, used to be a high school math teacher
c) Follow me on github/check out my npm. don't follow my twitter
II) Introduction
a) Topics today: heaps, quadtrees, graphs
b) What are data structures for?
1) Not just to store data - an unstructured blob can store data
3) Rather: store data with *efficient* insertion, access, and retrieval
On schoolTeacher resource:
{
_items: [
{
_id: 1234
...
_links: {
befriend/poolWith/etc: {
href: "/api/teacher/1234/friends",
method: "POST",
@peterkhayes
peterkhayes / Description
Created May 20, 2014 07:34
Making-Change problem with Dynamic Programming
// We write the problem as a table. Each row represents the amount of change to make,
// and the value at each column represents the highest coin we're allowed to use
// in making that amount of change
// That is to say, in row 5, col 2 is the number of ways to make 5 cents, using only the two lowest coins.
// To calculate the value of each square, we add the values of two other squares together -
// * The square to the left, which represents all possibilites using smaller coins.
// * The square as many rows above as the value of that coin.
// This is all possibilities after taking away one of this coin
// This method avoids having to ever call things recursively - we just build a big table!
@peterkhayes
peterkhayes / montecarlo.js
Last active August 29, 2015 13:56
monte carlo solution
var integrate = function() {
// return 42;
var iters = 2000;
var d = getGraphDimensions();
var width = d.x.max - d.x.min;
var height = d.y.max - d.y.min;
var xmin = d.x.min;