Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / 1-patience-sort.js
Last active February 6, 2017 19:51
Increasing Triplet Subsequence using Patience Sort algorithm (first lowest value, second lowest value, third). See https://en.wikipedia.org/wiki/Patience_sorting
var increasingTriplet = function(nums) {
var result = false;
// Patient sort algorithm.
var pile1 = 9007199254740992;
var pile2 = 9007199254740992;
for (var i=0; i<nums.length; i++) {
if (nums[i] <= pile1) {
// First lowest number.
@primaryobjects
primaryobjects / binary-tree.js
Last active February 6, 2017 17:03
Construct a binary tree and display sorted values (inorder traversal and preorder traversal). Fibonacci iterative and recursive. Demo at http://jsbin.com/gekusecole/edit?js,console
// noprotect
var createTree = function(arr) {
var head = null;
for (var i=0; i<arr.length; i++) {
if (!head) {
head = { value: arr[i] };
}
else {
// Search for location to add node.
@primaryobjects
primaryobjects / 1-single-number.js
Last active April 24, 2020 13:31
Given an array of integers, every element appears twice except for one. Find that single one.
/**
* @param {number[]} nums
* @return {number}
*/
var singleNumber = function(nums) {
var result = nums[0];
var memory1 = {};
var memory2 = {};
@primaryobjects
primaryobjects / island.png
Last active February 5, 2017 19:17
Given a 2D island map, find the perimeter of the island.
island.png
@primaryobjects
primaryobjects / reverse.js
Last active February 4, 2017 21:05
Reverse the letters in a string.
var reverseString = function(s) {
var result = '';
for (var i=s.length - 1; i>=0; i--) {
result += s[i];
}
return result;
};
@primaryobjects
primaryobjects / 1-reverse-vowels.js
Created February 4, 2017 21:01
Reverse Vowels of a String
/* Approach 1. Loop through string and store vowels on array, placeholders in string. Then loop through string again and replace in vowels in reverse order. */
var reverseVowels = function(s) {
var result = '';
var string = [];
var vowels = [];
// Start from front of string to find consonants and vowels.
for (var i=0; i<s.length; i++) {
var orig = s[i];
@primaryobjects
primaryobjects / 1-strstr.js
Last active July 15, 2022 01:43
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Substring search.
/* Same as basic method, but includes a look-ahead to speed up performance. */
var strStr = function(haystack, needle) {
var result = -1;
if (!needle.length) {
result = 0;
}
else if (needle.length) {
for (var i=0; i<haystack.length - needle.length + 1; i++) {
var nextStartingPos = 0;
@primaryobjects
primaryobjects / searchInsert.js
Last active August 8, 2020 00:09
Search Insert Position in a sorted array. Find the index of a value in a sorted array or return the index where it should be placed. Searches array using divide and conquer.
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var searchInsert = function(nums, target) {
var start = 0;
var end = nums.length - 1;
var index = Math.floor((end - start) / 2) + start;
@primaryobjects
primaryobjects / linkedList.js
Last active November 6, 2019 11:28
Reverse a Linked List in JavaScript.
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
@primaryobjects
primaryobjects / decode.js
Created January 26, 2017 19:46
Decode Ways a message can be decoded 'A' -> 1, 'B' -> 2, 'Z' -> 26 from a message of "12" can be "AB" (1 2) or "L" (12). Uses a DP array solution.
var numDecodings = function(s) {
if (!s.length || s[0] == '0') return 0;
// r2: decode ways of s[i-2] , r1: decode ways of s[i-1]
var r1 = 1, r2 = 1;
for (var i = 1; i < s.length; i++) {
// zero voids ways of the last because zero cannot be used separately
if (s[i] == '0') r1 = 0;
// possible two-digit letter, so new r1 is sum of both while new r2 is the old r1