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
// Returns the closest point from a list to a given point | |
// the naive approach runs in linear time, which is a quite slow if are calling this procedure a lot. | |
// The following is an approach using a KD-tree. It runs in <O(n*log n), O(log n)> time | |
// | |
// Example: | |
// var closetPoint = closetPointTree([[2, 3], [10, 17], [5, 6]]); | |
// closetPoint([10, 16]) | |
// -> [ 10, 17 ] | |
function closetPointTree(points) { |
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
// Group all overlaping intervals | |
// * * * * * * * | |
// This is an approach to a problem the engineers at Google Calandar/ Outlook probably faced. | |
// You have events that may overlap and you want to display them in such a way that | |
// they don't overlap with each other. One approach is to distribute them into columns. | |
// Each column has events that don't overlap with each other. | |
// Cost: O(n*log n) if the interval aren't sorted by the starting time, | |
// O(n) otherwise. | |
// Sample run: groupOverlapingIntervals([ [2, 5], [5, 6],[3, 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
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { | |
// I'm the background thread | |
dispatch_async(dispatch_get_main_queue()) { | |
// I'm the UI thread | |
} | |
} |
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
// Declare `~>` operator | |
infix operator ~> {} | |
// Define the `~>` operator | |
// | |
// Example: | |
// if inputStr ~> "^[a-zA-Z]*$" { | |
// println("\(inputStr) only has letters") | |
// } |
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 suffix array in O(n^2*log n); | |
// kinda too much, but now you can search for a pattern in O(n*log n) where `n` is number of characters in `str` | |
// | |
function searchPattern(str) { | |
var strLen = str.length; | |
var suffixArray = new Array(strLen); | |
var suffixes = new Array(strLen); | |
for (var i = strLen-1; i >= 0; i--) { | |
suffixes[i] = [i, str.substr(i)]; |
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
/** | |
* Applies HTML formatting to a raw string | |
* Runtime: O(n) | |
* | |
* @param str {String} A raw string | |
* @param formats {Array} An array of formats where each one has the following structure: [startOffset, endOffset, tagName] | |
* @return formattedString {String} | |
**/ | |
function format(str, formats) { | |
var formatsLen = formats.length; |
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
/** | |
* The following algorithm maximizes the size of the thumbnails in a grid with an arbitrary width | |
* Some examples of this algorithm are: | |
* https://www.flickr.com/search/?q=landscapes | |
* https://www.google.com/search?q=landscapes&source=lnms&tbm=isch | |
* | |
* | |
* @param width {Number} the width of the viewport in the same unit as the size of the images | |
* @param maxRowHeight {Number} the maximum allowed size of the row | |
* @param images {Array} containing the size of each image in the following structure: [width, height] |
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 newRange(startElement, startOffset, endElement, endOffset) { | |
var range = document.createRange(); | |
range.setStart(startElement, startOffset); | |
range.setStart(endElement, endOffset); | |
return range; | |
} | |
function selectRange(range) { | |
window.getSelection().addRange(range); | |
} |
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
// https://oj.leetcode.com/problems/word-ladder/ | |
function replaceChar(str, index, character) { | |
return str.substr(0, index) + character + str.substr(index + 1); | |
} | |
function ladderLength(start, end, dict) { | |
var dictTable = {}; | |
var endTable = {}; | |
for (var j = 0; j < end.length; j++) { |
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
/** | |
* Grabs a lock and ensures mutual exclusion while calling `closure` | |
* Example: | |
* synced(self) { | |
* // Code | |
* } | |
*/ | |
func synced(lock: AnyObject, closure: () -> ()) { | |
objc_sync_enter(lock) | |
closure() |