Skip to content

Instantly share code, notes, and snippets.

View jtrein's full-sized avatar

Jeremiah Naylor-Trein jtrein

View GitHub Profile
@jtrein
jtrein / greedyAlgorithm.js
Last active February 1, 2020 17:33
Greedy Algorithm in JavaScript - Max Profit on Stock Prices (with comments)
/**
* Greedy Algorithm (maximum daily profit from stock sale) - "let me see what my options are, first..."
*
* Overview:
* ---------
* By using Greedy Algorithms we can pass over the data once (O(n) time), storing values we find to be optimal, per our criteria, by
* comparing them to current values. We have to go over the entire set to do this, but we only have to do it once - yay!
*
* From Wikipedia: "A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal
* choice at each stage with the hope of finding a global optimum."
@jtrein
jtrein / mergeSortAlgorithm.js
Last active December 28, 2016 22:15
Merge Sort Algorithm in JavaScript (with Comments)
/**
* Merge Sort Algorithm: "divide and conquer", "break you down in order to build you back up"
* Overview:
* ---------
* A recursive sorting algorithm where we 1) divide an unsorted array in half (dismantle big array into tiny sub-arrays)
* until we reach atomic values (< 2 - it's never 0), then we 2) sort the adjacent sub-arrays (left, right) and
* 3) merge the sorted sub-array values (put things back together) after each iteration
*
* Time Complexity (worst case):
* -----