Skip to content

Instantly share code, notes, and snippets.

View keif's full-sized avatar

Keith Baker keif

View GitHub Profile
@keif
keif / gist:14e7cedbe0a98112ad30
Created April 1, 2015 05:06
Jekyll Command to include Drafts and Utilize dev config.
bundle exec jekyll serve --drafts --config _config.yml,_config_dev.yml --watch
@keif
keif / _config_dev.yml
Created April 1, 2015 05:10
A Dev YAML File example for Jekyll
title: Baker's GitHub Repo DEVELOPMENT
url: http://localhost:4000
baseurl: ""
@keif
keif / rotate
Created April 10, 2015 04:18
Problem: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
// Problem: Rotate an array of n elements to the right by k steps.
// For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
// How many different ways do you know to solve this problem?
var array = [1,2,3,4,5,6,7];
var result = [5,6,7,1,2,3,4];
// Solution 1 - Intermediate Array
function rotate(nums, k) {
var result = [],
@keif
keif / morse encode and decode
Last active August 29, 2015 14:19
A generic morse code encode/decode exercise.
var charCodes= {
"a": ". _",
"b": "_ . . .",
"c": "_ . _ .",
"d": "_ . .",
"e": ".",
"f": ". . _ .",
"g": "_ _ .",
"h": ". . . .",
"i": ". .",
@keif
keif / more encode and decode
Last active August 29, 2015 14:19
A generic morse encode/decode exercise utilizing filters and map instead of regex matching.
var charCodes= {
"a": ". _",
"b": "_ . . .",
"c": "_ . _ .",
"d": "_ . .",
"e": ".",
"f": ". . _ .",
"g": "_ _ .",
"h": ". . . .",
"i": ". .",
@keif
keif / evalRPN
Created April 21, 2015 06:00
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. For example: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
// ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
// ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
var arr1 = ["2", "1", "+", "3", "*"]; // 9
var arr2 = ["4", "13", "5", "/", "+"]; // 6
// lifted from jQuery
var inArray = function (elem, arr, i) {
var len;
if (arr) {
@keif
keif / Manacher
Created April 22, 2015 05:09
A VERY INCOMPLETE AND UNTESTED JavaScript code rip of Manacher's algorithm. I want to get this into node to actually play with it.
/**
* Computes the longest palindromic substring in linear time
* using Manacher's algorithm.
*
* Credits: The code is lifted from the following excellent reference
* http://www.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html
**/
var Manacher = (function () {
var palindromic = [], // p[i] = length of longest palindromic substring of t, centered at i
@keif
keif / wordbreak1
Created April 22, 2015 05:34
// Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. // For example, given var string = "leetcode", dict = ["leet", "code"]; // Return true because "leetcode" can be segmented as "leet code".
// Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
// For example, given
var string = "leetcode",
dict = ["leet", "code"];
// Return true because "leetcode" can be segmented as "leet code".
// 1. Naive Approach
// This problem can be solve by using a naive approach, which is trivial. A discussion can always start from that though.
// Time is O(n^2) and exceeds the time limit.
var wordBreak = function (string, dict) {
@keif
keif / wordbreak2
Created April 22, 2015 05:46
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats", "and", "sand", "dog"], the solution is ["cats and dog", "cat sand dog"].
// Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each
// word is a valid dictionary word. Return all such possible sentences. For example, given:
// s = "catsanddog", dict = ["cat", "cats", "and", "sand", "dog"], the solution is ["cats and dog", "cat sand dog"].
// need to track the actual matched words
var wordBreak = function (string, dict) {
//create an array of ArrayList<String>
var dp = [string.length + 1];
dp[0] = [];
@keif
keif / unique
Created April 24, 2015 04:52
Old School vs. New School: Double for loop vs. a dictionary method for getting the unique values from array.
// old school double loop
// quadratic O(n^2) inefficiency
var unique = function(array) {
var arr = [],
len = array.length;
for (var idx = 0; idx < len; i++) {
for(var jdx = i + 1; jdx < len; jdx++) {
if (array[idx] === array[jdx]) {
j = ++i;