Skip to content

Instantly share code, notes, and snippets.

View optimistiks's full-sized avatar

Max optimistiks

View GitHub Profile
@optimistiks
optimistiks / course-schedule-2.js
Last active August 21, 2023 21:22
Let’s assume that there are a total of n courses labeled from 0 0 to n−1 . Some courses may have prerequisites. A list of prerequisites is specified such that if Prerequisites i ​ =a,b , you must take course b before course a . Given the total number of courses n and a list of the prerequisite pairs, return the course order a student should take…
export function findOrder(n, prerequisites) {
const adjList = {};
// init adj list
for (let i = 0; i < n; ++i) {
adjList[i] = [];
}
// build adj list from prereqs
for (let i = 0; i < prerequisites.length; ++i) {
@optimistiks
optimistiks / course-schedule.js
Created August 21, 2023 21:22
There are a total of numCourses courses you have to take. The courses are labeled from 0 to numCourses - 1. You are also given a prerequisites array, where prerequisites[i] = [a[i], b[i]] indicates that you must take course b[i] first if you want to take the course a[i]. For example, the pair [ 1 , 0 ] [1,0] indicates that to take course 1 1 , y…
export function canFinish(numCourses, prerequisites) {
const adjList = {};
// initialize the adj list
for (let i = 0; i < numCourses; ++i) {
adjList[i] = [];
}
// build the adj list from prereq, where each prereq is a pair [to, from]
// where from is a course that must be taken before course to
@optimistiks
optimistiks / find-all-possible-recipes.js
Created August 21, 2023 22:40
A list of recipes a chef can prepare from the supplied items is given. Ingredients required to prepare a recipe are mentioned in the ingredients list. The ith ​ recipe has the name recipes i, and you can create it if you have all the needed ingredients from the ingredients i ​ list. A recipe may be listed as an ingredient in a different recipe. …
export function findRecipes(recipes, ingredients, supplies) {
// count dependencies of each recipe
const deps = {};
const adjList = [];
// initialize adj list and deps map from recipes and supplies
recipes.forEach(recipe => {
adjList[recipe] = [];
deps[recipe] = 0;
});
@optimistiks
optimistiks / set-matrix-zeros.js
Created October 16, 2023 16:48
Given a matrix, mat, if any element within the matrix is zero, set that row and column to zero.
function setMatrixZeros(mat) {
let frow = false;
let fcol = false;
const rows = mat.length;
const cols = mat[0].length;
// if any element in the first row or in the first column is 0, set frow / fcol to true
for (let col = 0; col < cols; ++col) {
@optimistiks
optimistiks / rotate-matrix.js
Created November 1, 2023 15:25
Given an n×n matrix, rotate the matrix 90 degrees clockwise. The performed rotation should be in place, i.e., the given matrix is modified directly without allocating another matrix.
function rotateImage(matrix) {
let l = 0;
let r = matrix.length - 1;
while (l < r) {
for (let i = 0; i < (r - l); ++i) {
const t = l;
const b = r;
const topLeft = matrix[t][l + i];
matrix[t][l + i] = matrix[b - i][l];
matrix[b - i][l] = matrix[b][r - i];
@optimistiks
optimistiks / spiral-matrix.js
Created November 1, 2023 18:26
Given an m×n matrix, return an array containing the matrix elements in spiral order, starting from the top-left cell.
function spiralOrder(matrix) {
const result = [];
// initialize direction variable (1 or -1)
let dir = 1;
// current row
let row = 0;
// current column (we have to start "outside" since we don't want to go out of bounds)
// current row starts at 0 and not -1 because our first step is to move right, and it's changing columns
// after moving right our first row is complete, so row=0 is actually "outside", just as col=-1 is.
@optimistiks
optimistiks / where-will-the-ball-fall.js
Created November 4, 2023 21:08
You have n balls and a 2D grid of size m×n representing a box. The box is open on the top and bottom sides. Each cell in the box has a diagonal that can redirect a ball to the right or the left. You must drop n balls at each column’s top. The goal is to determine whether each ball will fall out of the bottom or become stuck in the box.
export function findExitColumn(matrix) {
// number of balls = number of columns = length of the first row since all rows are equal length
const balls = matrix[0].length;
// initialize result array, size = number of balls, initial values = initial columns of balls (0 for ball 0, n for ball n)
const result = Array(balls).fill(null).map((_, i) => i);
// iterate matrix row, from 0 to last row
for (let row = 0; row < matrix.length; ++row) {
// at each row, iterate ball positions
@optimistiks
optimistiks / basic-calculator.js
Created November 18, 2023 17:45
Given a string containing an arithmetic expression, implement a basic calculator that evaluates the expression string. The expression string can contain integer numeric values and should be able to handle the “+” and “-” operators, as well as “()” parentheses.
export function calculator(expression) {
// so we have a stack, and an expression which is a string
// we iterate one character from the expression at a time
// so we need a variable (current number) and another (result) and another (sign)
// and a stack
const stack = [];
let currentNumber = "";
let sign = 1;
@optimistiks
optimistiks / remove-adjacent-duplicates.js
Created November 19, 2023 19:16
You are given a string consisting of lowercase English letters. Repeatedly remove adjacent duplicate letters, one pair at a time. Both members of a pair of adjacent duplicate letters need to be removed.
export function removeDuplicates(string) {
const stack = [];
for (const char of string) {
if (stack[stack.length - 1] === char) {
stack.pop();
} else {
stack.push(char);
}
}
@optimistiks
optimistiks / min-remove-parentheses.js
Created November 19, 2023 19:57
Given a string, s, that may have matched and unmatched parentheses, remove the minimum number of parentheses so that the resulting string represents a valid parenthesization.
export function minRemoveParentheses(s) {
const split = s.split("");
const stack = [];
for (let i = 0; i < split.length; ++i) {
const char = split[i];
if (
char === ")" &&
stack.length > 0 &&
stack[stack.length - 1][0] === "("