This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var extend = function(obj) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
for (var i = 0, len = args.length; i < len; i++) { | |
for (var prop in args[i]) { | |
obj[prop] = args[i][prop]; | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<snippet> | |
<content> | |
<![CDATA[ | |
/** | |
* ${1:Method Description} | |
* | |
* @param {${2:argumentType}} ${3:argumentName} ${3:argumentDesc} | |
* @return {${4:returnType}} | |
*/ | |
]]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(root, factory) { | |
if (typeof exports == 'object') { | |
// I'm Node | |
module.exports = factory(require('./lib')); | |
} else { | |
// I'm the Browser | |
factory(root.lib); | |
} | |
}(this, function(lib) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Download the lastest from remote without trying to merge | |
# or rebase and reset the master branch to what was fetched | |
git fetch --all && git reset --hard origin/master | |
# Show files in the index that are unmerged | |
git ls-files -u | |
# Clone a branch | |
git clone -b Branch-name Repo-Url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Build a tree using a compacted object with no pointer for the left/right children | |
function buildTree(A) { | |
// Shuffle the array. so we gain a more balanced search tree | |
A.sort(function() { return parseInt(Math.random()*3) - 1; }); | |
var tree = {'0': A[0]}, current = 1; | |
var inserted, node, treeSize = 1; | |
// This will take O(N * log N) if we have a balanced tree | |
// the worst case cost will be O(N^2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getValue(str) { | |
switch (str) { | |
case 'one': | |
return 1; | |
case 'two': | |
return 2; | |
case 'three': | |
return 3; | |
case 'four': | |
return 4; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function longestPrefix(str) { | |
// create a table of size equal to the length of `str` | |
// table[i] will store the prefix of the longest prefix of the substring str[0..i] | |
var table = new Array(str.length); | |
var maxPrefix = 0; | |
// the longest prefix of the substring str[0] has length | |
table[0] = 0; | |
// for the substrings the following substrings, we have two cases |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Construct a table with table[i] as the length of the longest prefix of the substring 0..i | |
function longestPrefix(str) { | |
// create a table of size equal to the length of `str` | |
// table[i] will store the prefix of the longest prefix of the substring str[0..i] | |
var table = new Array(str.length); | |
var maxPrefix = 0; | |
// the longest prefix of the substring str[0] has length | |
table[0] = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For more info about indexed trees visit | |
// http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees | |
// | |
// Sample run: | |
// var tree = new IndexedTree([2, 5, 6, 9]) | |
// tree.getSumIn(2, 3) -> 15 | |
// | |
// Why this is cool? It gives the sum of the values of a subarray in O(log n) time, | |
// the update of a value also runs in O(log n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// How fast can you get all the intervals that overlap with another interval? | |
// WTF.. who cares. Well if you are given the task to find all the stories | |
// from a facebook timeline visible to the user, | |
// how would you do that without blocking the user's scrolling? | |
// Imagine, you mark the stories as read and notify the server that the user saw those stories. | |
// It turns out you can achieve this task in log time using a segment tree. | |
// More about segment trees: | |
// http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor#Segment_Trees | |
// Sample: |
OlderNewer