Skip to content

Instantly share code, notes, and snippets.

View jykim16's full-sized avatar

Jonathan Kim jykim16

View GitHub Profile
//did not complete
/**
* @param {number[][]} envelopes
* @return {number}
*/
var maxEnvelopes = function(envelopes) {
let max = 0;
let sortedByHeightEnvelopes = envelopes.sort((a,b)=>{return a[0] < b[0]});
let recurseHeight = (currentHeight, remainingEnvelopes, currentCount) => {
@jykim16
jykim16 / CashRegister
Created September 18, 2017 17:12
cashinator 3000
class CashRegister {
constructor(csv, salesTax, startingBalance) {
this.balance = startingBalance;
this.transactions_report = [];
this.items_report = [];
this.currentTransaction = {};
}
viewBalance () {
//return balance
}
@jykim16
jykim16 / message-bus
Created September 7, 2017 17:54
subscribe publish module
let messageBus = {};
class Channel{
constructor() {
this.topicSubscriptionCache = {};
}
publish(topic, payload) {
if (payload) {
//find array of callbacks for topic in cache
if(this.topicSubscriptionCache[topic]) {
@jykim16
jykim16 / vowel-doubler
Created September 5, 2017 16:57
Given an array of characters, return the array with every vowel doubled. For example:
**/Constraints
The challenge in this problem is in meeting its (arbitrary) constraints:
Do not convert into strings or manipulate strings at all.
Do not create any other data structures.
In particular, don't instantiate a new array.
The big-O of the solution should be O(n). **/
let vowelDoubler = (letters) => {
@jykim16
jykim16 / productExceptSelf
Created August 31, 2017 16:39
Product of Array Except Self
var productExceptSelf = function(nums) {
let zeroCount = 0;
let productWithoutZero = 1;
let product = nums.reduce((accum, num)=>{
if(num === 0) {zeroCount++}
if(num !== 0) {productWithoutZero *= num}
return accum * num;
}, 1);
return nums.map((num, i)=>{
var kthSmallest = function(root, k) {
let kSmallestElement = NaN;
let findLastKNodes = (node, isRightNode, parentCountFromLeftTree) => {
var nthElement = 1;
if(node.left) {
nthElement += findLastKNodes(node.left, isRightNode);
}
if (node.right) {
findLastKNodes(node.right, true, nthElement);
nthElement += 1;
@jykim16
jykim16 / Path-to-target-sum (practice interview)
Last active August 10, 2017 16:27
Tree has path to target sum? You are given a binary tree whose nodes all have integer values (both positive and negative). Given some target sum (say, 14), return true if there is any path starting from the root and ending in a leaf, such that adding up all the values along the path equals the given sum. const hasPathToSum = function(node, targe…
class Tree {
constructor(name, val) {
this.value = val;
this.children = [];
this.name = name;
}
addChild(child) {
this.children.push(child)
}
}
@jykim16
jykim16 / cashamount class
Created August 3, 2017 16:59
Prompt: tackling floating-point imprecision with the CashAmount class
class CashAmount{
constructor(amount) {
this.value = amount;
}
//converts amount to pennies
totalInPennies() {
return this.value * 100;
}
@jykim16
jykim16 / gist:0ee5984f7143e5a62c879c5a6bef2bcc
Last active July 21, 2017 00:44
largest-level-of-tree
const findLargestLevel = function(node) {
let currLevel = [node]
let i = 0;
let index = [[0, node.value]];
while(currLevel.length > 0) {
nextLevel = [];
i++;
//function that moves currlevl children to nextlevel children
for(let parent = 0; parent < currLevel.length; parent++) {
//nextLevel.push(children)
@jykim16
jykim16 / gist:5a6828d178127e0087158fcd8d5196b7
Last active December 24, 2021 16:49
118. Pascal's Triangle (LeetCode)
var generate = function(numRows) {
var triangle = [];
var counter = 0;
while(counter < numRows){
var lastRow = triangle[triangle.length - 1];
var newRow = [];
if (!lastRow) {newRow.push(1);
} else {
lastRow.forEach(function(num, i){
for(var j = i; j < i + 2; j++){