Skip to content

Instantly share code, notes, and snippets.

View PantherHawk's full-sized avatar

Alexander Rosenthal PantherHawk

View GitHub Profile

Requirements

Given a number, return the roman numeral.

Strategy

We're going back in time to the third century! Subtract the integer value of a roman numeral from the input value until the value is less than that the roman numeral value. Then move to the next roman numeral.

Justification of strategy

Roman numerals are just sums of particular integers. We want to find the sum created by the discrete number of integers provided by roman numerals. So we have to break the integer down, starting with our biggest building blocks. We only need to move to a smaller roman numeral if after dividing our input by the roman numeral there's a remainder. We handle the thousands, and then the hundreds, and finally the units. Once we find a value that is smaller or equal to our number, we will push the matching letter to our solution and subtract this value from our number.

Define test cases

  • intToRoman(123) should return "CXXIII"
var intToRoman = function(num) {
// store numerals
let romanNums = '';
// make a graph mapping an array of integers to an array of roman numerals such that an index will map the two as we iterate through the integers array.
let integers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
let romans = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
// iterate over integers
for (var i = 0; i < integers.length; i++) {
// while the remainder of the input number divided by the roman numeral's integer is greater than than the integer at i ...
while(num%integers[i] < num) {

Requirements

Return the area of the largest rectangle formed by two heights in an array of heights.

Strategy

Take two pointers, one at the beginning and one at the end of the array. The difference between these to indexes constitutes the area's width. Store the maximum area obtained till now. At every iteration, we find out the area formed between them, update maximum area and move the pointer pointing to the shorter line towards the other end by one step.

Justification of strategy

The area formed by two lines will always be limitted by the shorter of the two. When the two pointers are at their starting positions, 0 and array.length -1 respectively, width is greatest. But moving the pointer and decrementing the width could increase the height and make up for the lost width. So we look for larger lines by moving the pointer and compare the area of the new rectangle formed to the stored area.

Define test cases

  • [1, 2] should return 1
  • [1,8, 6, 2, 5, 4, 8, 3, 7] should return 49
var maxArea = function(height) {
// declare pointer variables to move from front and back
// declare area to store max area
let maxarea = 0;
let forward = 0;
let back = height.length - 1;
// while the pointer moving forward is less than the pointer moving backwards ...
while(forward < back) {
// compare the area stored to the area formed by width and the shorter of the two lines indexed by the pointers.
maxarea = Math.max(maxarea, Math.min(height[forward], height[back]) * (back - forward));

Requirements

Return the median of two sorted arrays.

Strategy

Concatenate arrays, sort, then find median.

Justification of strategy

We want to find the median of the set of numbers provided by both arrays. Assume we instead tried to find the medians of each array seperately and then sought the median of those medians.

@PantherHawk
PantherHawk / Leet Code: median-of-two-sorted-arrays
Created December 5, 2017 13:57
Median of two sorted arrays implementation. For Leet Code.
var findMedianSortedArrays = function(nums1, nums2) {
// new tactic, merge sort the two arrays, and then find the median.
let sorted = mergeSort(nums1.concat(nums2));
if (sorted.length % 2 === 0) {
return handleEven(sorted);
}
return handleOdd(sorted);
}
function mergeSort(arr) {

Requirements

Strategy

Advance two pointers along the linked list.

  • Pointer A moves one node at a time.
  • Pointer B moves two nodes at a time.
https://repl.it/@pantherhawk22/2D-Matrix-Rotation
// rotate 2d grid 90 degrees
// o: 2d array rotated 90 degrees, counter clockwise
// i: 2d array, width = length
// c: no worse the O(n^2)
// PLAN:
// reverse order of rows
// replace the last column elements of each row with elements in the first row, until you reach the next to last
//to go into a DOM trees we use document.childNodes
// we have to make a tree structure of the DOM
// traverse the DOM, and make a new tree for every node, and print the tree
class Tree {
constructor(val) {
this.val = val;
this.children = [];
}
decimalZip = (A, B) => {
if ( typeof A !== "number" || typeof B !== "number" ) {
return undefined;
}
var a = JSON.stringify(A).split('')
var b = JSON.stringify(B).split('')
var c = [];
var longer = a.length > b.length ? a : b;
for (var i = 0; i < longer.length; i++) {
if (a.length) {