Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
:octocat:
Focusing

Miguel Urena tatsuyax25

:octocat:
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / minimumTotal.js
Created September 25, 2025 17:26
Given a triangle array, return the minimum path sum from top to bottom. For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the
/**
* @param {number[][]} triangle
* @return {number}
*/
var minimumTotal = function(triangle) {
// Start from the second-to-last row and move upward
for (let row = triangle.length - 2; row >= 0; row--) {
for (let col = 0; col < triangle[row].length; col++) {
// For each element, choose the minimum of the two adjacent numbers in the row below
// and add it to the current element
@tatsuyax25
tatsuyax25 / fractionToDecimal.js
Created September 24, 2025 18:30
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. If multiple answers are possible, return any of them.
/**
* @param {number} numerator
* @param {number} denominator
* @return {string}
*/
var fractionToDecimal = function(numerator, denominator) {
if (numerator === 0) return "0";
let result = "";
@tatsuyax25
tatsuyax25 / maxFrequencyElements.js
Created September 22, 2025 16:53
You are given an array nums consisting of positive integers. Return the total frequencies of elements in nums such that those elements all have the maximum frequency. The frequency of an element is the number of occurrences of that element in the a
/**
* @param {number[]} nums
* @return {number}
*/
var maxFrequencyElements = function(nums) {
// Create an empty object to store the frequency of each number.
const map = {};
// Initialize the maximum frequency to 0.
let maxF = 0;
@tatsuyax25
tatsuyax25 / MovieRentingSystem.js
Created September 21, 2025 16:36
You have a movie renting company consisting of n shops. You want to implement a renting system that supports searching for, booking, and returning movies. The system should also support generating a report of the currently rented movies. Each movie
/**
* @param {number} n
* @param {number[][]} entries
*/
// Constructor for the MovieRentingSystem
var MovieRentingSystem = function(n, entries) {
// Maps each movieId to a priority queue of shops offering it
this.movieIdToShopInfo = new Map();
// Maps "shopId|movieId" to its price
@tatsuyax25
tatsuyax25 / Router.js
Created September 20, 2025 16:19
Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes: source: A unique identifier for the machine that generated the packet. destination: A unique identifier for
/**
* Router constructor initializes the router with a memory limit.
* @param {number} memoryLimit - Maximum number of packets the router can hold.
*/
var Router = function(memoryLimit) {
this.size = memoryLimit; // Maximum capacity of the router.
this.packets = new Map(); // Stores packets keyed by encoded value.
this.cnt = new Map(); // Maps destination to sorted timestamps for quick count queries.
this.queue = []; // Maintains insertion order of packets.
this.head = 0; // Pointer to the next packet to forward.
@tatsuyax25
tatsuyax25 / Spreadsheet.js
Created September 19, 2025 19:13
A spreadsheet is a grid with 26 columns (labeled from 'A' to 'Z') and a given number of rows. Each cell in the spreadsheet can hold an integer value between 0 and 105. Implement the Spreadsheet class: Spreadsheet(int rows) Initializes a spreadsheet
/**
* Spreadsheet constructor
* @param {number} rows - Number of rows in the spreadsheet (not used in this implementation)
*/
var Spreadsheet = function (rows) {
// Initialize a Map to store cell values keyed by cell name (e.g., "A1", "B2")
this.mp = new Map();
};
/**
@tatsuyax25
tatsuyax25 / TaskManager.js
Created September 18, 2025 16:25
There is a task management system that allows users to manage their tasks, each associated with a priority. The system should efficiently handle adding, modifying, executing, and removing tasks. Implement the TaskManager class: TaskManager(vector<v
/**
* @param {number[][]} tasks
*/
var TaskManager = function (tasks) {
// Initialize a max-heap (priority queue) with custom comparator:
// - Higher priority comes first
// - If priorities are equal, higher taskId comes first
this.pq = new PriorityQueue((a, b) => {
if (a.priority === b.priority) {
return b.taskId - a.taskId; // tie-breaker: higher taskId wins
@tatsuyax25
tatsuyax25 / FoodRatings.js
Created September 17, 2025 21:16
Design a food rating system that can do the following: Modify the rating of a food item listed in the system. Return the highest-rated food item for a type of cuisine in the system. Implement the FoodRatings class: FoodRatings(String[] foods, Strin
/**
* Initializes the FoodRatings system.
* @param {string[]} foods - List of food names.
* @param {string[]} cuisines - Corresponding cuisine for each food.
* @param {number[]} ratings - Initial rating for each food.
*/
var FoodRatings = function (foods, cuisines, ratings) {
// Maps cuisine → priority queue of {food, rating}
this.cuisineMapPriority = new Map();
@tatsuyax25
tatsuyax25 / replaceNonCoprimes.js
Created September 16, 2025 17:07
You are given an array of integers nums. Perform the following steps: Find any two adjacent numbers in nums that are non-coprime. If no such numbers are found, stop the process. Otherwise, delete the two numbers and replace them with their LCM (Leas
/**
* @param {number[]} nums
* @return {number[]}
*/
var replaceNonCoprimes = function(nums) {
// Helper function to compute GCD using Euclidean algorithm
const gcd = (a, b) => {
while (b !== 0) {
let temp = b;
b = a % b;
@tatsuyax25
tatsuyax25 / canBeTypedWords.js
Created September 15, 2025 17:33
There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly. Given a string text of words separated by a single space (no leading or trailing spaces) and a string brokenLetters of all distinct
/**
* @param {string} text
* @param {string} brokenLetters
* @return {number}
*/
var canBeTypedWords = function(text, brokenLetters) {
// Step 1: Split the input text into individual words
const words = text.split(" ");
// Step 2: Convert brokenLetters into a Set for fast lookup