Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / reverse-double-linked-list.java
Created March 3, 2017 22:08
Reverse a doubly linked list. Double linked list reversal. Reverse a double-linked list.
/*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
Node is defined as
class Node {
int data;
Node next;
Node prev;
}
*/
@primaryobjects
primaryobjects / palindrome-index.js
Created March 3, 2017 22:02
Determine the index of the character whose removal will create a palindrome.
/* Method: Traverse through half the string and compare the left-side letter with the right-side letter (hence, traversing only half the string).
If the left letter does not equal the right letter, we have found the location of the removal letter to form a palindrome.
To determine which side to remove, check if the next left-side letter (i+1) matches the right-side AND we result in a complete palindrome.
Otherwise, check if the next right-side letter (str.length - 2 - j) matches the left-side AND we result in a complete palindrome.
*/
function isPalindrome(str) {
var result = true;
for (var i=0; i<str.length / 2; i++) {
@primaryobjects
primaryobjects / 010.js
Created March 3, 2017 03:23
Count steps to remove all occurrences of 010 in string.
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
@primaryobjects
primaryobjects / 1-linked-list-merge-point.java
Created March 3, 2017 03:03
Find the merge point of 2 linked lists that connect together.
/* Method 1: Step through each linked list. When the pointer reaches the end of the list, set it to the head of the other list.
The pointers will eventually collide. Runtime O(n), Space O(1)
*/
/*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
Node is defined as
class Node {
int data;
@primaryobjects
primaryobjects / word-pattern.js
Created March 2, 2017 19:45
Given a pattern abba find if a string of words follows the same pattern.
/* Method: Solution uses a hash of each word and its assigned pattern symbol,
along with a second hash to record pattern symbols that have already been used.
*/
/**
* @param {string} pattern
* @param {string} str
* @return {boolean}
*/
var wordPattern = function(pattern, str) {
@primaryobjects
primaryobjects / roman-numerals.js
Created March 2, 2017 19:32
Convert a roman numeral string into an integer.
/* Method: Works by using a map of symbol values and walking backwards in the string checking the current symbol with the one to the right of it.
If the symbol to the right is greater, then we subtract the current symbol (ie., IV IX etc).
Otherwise, we just add the current symbol.
See also http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm
*/
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
@primaryobjects
primaryobjects / ugly-number.js
Created March 2, 2017 18:12
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
var isUgly = function(num) {
// Keep dividing by 2, 3, 5 until we arrive at 1.
var arr = [2, 3, 5];
arr.forEach(function(prime) {
while (num && num % prime === 0) {
num /= prime;
}
});
return num === 1;
@primaryobjects
primaryobjects / heater-radius.js
Last active March 2, 2017 17:53
Find the minimum radius of heaters so that all houses on x-axis array will be covered by heaters.
/* This solution goes through each house, finds its closest left and right heater, and then gets the distance for the closest heater.
If the closest heater is the new maximum range, we keep it. Otherwise, we keep the prior maximum range found.
If a house is to the left of all heaters, we use the distance between the house and heaters[0].
If a house is to the right of all heaters, we use the distance between the house and heaters[heaters.length - 1].
*/
var findRadius = function(houses, heaters) {
var result = 0;
if (houses && houses.length) {
@primaryobjects
primaryobjects / 1-linked-list-cycle-detect.java
Last active August 1, 2018 19:37
Detect a cycle in a linked list, using hashcode and using slow/fast method.
/* Method 1: Using hashCode() to store previously seen nodes. */
/*
Detect a cycle in a linked list. Note that the head pointer may be 'null' if the list is empty.
A Node is defined as:
class Node {
int data;
Node next;
}
@primaryobjects
primaryobjects / linked-list-duplicates.java
Created March 2, 2017 15:47
Remove duplicates from a linked list.
/*
Node is defined as
class Node {
int data;
Node next;
}
*/
Node RemoveDuplicates(Node head) {
Hashtable<Integer, Integer> hash = new Hashtable<Integer, Integer>();