Skip to content

Instantly share code, notes, and snippets.

View haase1020's full-sized avatar
Coding with joy!

Mandi Haase haase1020

Coding with joy!
View GitHub Profile
@haase1020
haase1020 / higher order functions
Created July 11, 2022 20:43
simple example of a higher order function
enum SIZE {
small = 'small',
medium = 'medium',
large = 'large'
}
function calculateAge(operation: Function, initialVal: number, factor: boolean | SIZE) {
const age = operation(initialVal, factor)
return age;
}
@haase1020
haase1020 / sameTreeRecursive.js
Created December 6, 2021 10:57
same tree leetcode #100
// recursive solution: time complexity: O(n) | space complexity: O(h) where h is height of tree (recursion stack)
var isSameTree = function (p, q) {
if (!p && !q) {
return true;
}
if (!p || !q || p.val !== q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};
@haase1020
haase1020 / sameTreeIterative. js
Created December 6, 2021 10:55
same tree leetcode #100
// Iterative solution: time complexity: O(n) | space complexity: O(n)
var isSameTree = function (p, q) {
const q1 = [];
q1.push(p);
const q2 = [];
q2.push(q);
while (q1.length && q2.length) {
const curr1 = q1.shift();
const curr2 = q2.shift();
@haase1020
haase1020 / binaryTreeInorderTraversal.js
Created December 6, 2021 10:32
binary tree inorder traversal
// https://leetcode.com/problems/binary-tree-inorder-traversal/
const dft = (root, result) => {
if (!root) return;
dft(root.left, result);
result.push(root.val);
dft(root.right, result);
return result;
};
@haase1020
haase1020 / fibonacciRecursive
Created December 6, 2021 09:51
fibonacci recursive
// multiple recursion: one than one recursive call per iteration
// time complexity: O(2^n) or exponential time
// space complexity: O(n)
function fibonacci(n) {
if ((n === 1) | (n === 2)) return 1;
return fibonacci(n - 1) + fibonacci(n - 2); // multiple calls of fibonacci
}
// better but still slower than iterative
function fibonacciMemoized(n, cache = []) {
@haase1020
haase1020 / factorialIterative.js
Created December 6, 2021 09:46
factorial iterative
function factorial(n) {
let answer = 1;
if (n == 0 || n == 1) {
return answer;
} else {
for (var i = n; i >= 1; i--) {
console.log("running"); // should log 5 times
answer = answer * i;
}
return answer;
@haase1020
haase1020 / factorialRecursive.js
Last active December 6, 2021 09:47
factorial recursive
function factorial(n) {
if (n == 0 || n == 1) {
return 1;
} else {
console.log("running"); // should run 5 times
return n * factorial(n - 1);
}
}
console.log(factorial(5)); //5*4*3*2*1 = 120
@haase1020
haase1020 / cleanHouseOptimized
Last active December 6, 2021 08:08
clean house recursive optimized
const todoList = [
"pick up the floor",
"clear the table",
"put away dishes",
"sweep",
"clean the bathrooms",
"dust",
"mop",
];
@haase1020
haase1020 / cleanHouse.js
Last active December 6, 2021 08:06
clean house recursive function
const todoList = [
"pick up the floor",
"clear the table",
"put away dishes",
"sweep",
"clean the bathrooms",
"dust",
"mop",
];
@haase1020
haase1020 / symmetricTreeRecursive.js
Last active December 4, 2021 10:49
#101 LeetCode Symmetric Tree Recursive Solution
var isSymmetric = function (root) {
if (!root) return "no tree was provided 🤔"; // return true
return dfs(root.left, root.right);
function dfs(leftNode, rightNode) {
console.log("running this code block"); // this will run at most for the total number of nodes in the tree
if (!leftNode && !rightNode) {
return "you have a perfectly symmetric tree! 🌲"; //return true
}
if (