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 / 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
@tatsuyax25
tatsuyax25 / spellchecker.js
Created September 14, 2025 16:41
Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word. For a given query word, the spell checker handles two categories of spelling mistakes: Capitalization: If the query matches a word in the wordlist
/**
* @param {string[]} wordlist
* @param {string[]} queries
* @return {string[]}
*/
var spellchecker = function(wordlist, queries) {
// Helper function to normalize vowels in a word
const normalizeVowels = (word) => {
return word.toLowerCase().replace(/[aeiou]/g, '*');
};
@tatsuyax25
tatsuyax25 / maxFreqSum.js
Created September 13, 2025 21:14
You are given a string s consisting of lowercase English letters ('a' to 'z'). Your task is to: Find the vowel (one of 'a', 'e', 'i', 'o', or 'u') with the maximum frequency. Find the consonant (all other letters excluding vowels) with the maximum
/**
* @param {string} s
* @return {number}
*/
var maxFreqSum = function(s) {
// Step 1: Define vowels for easy lookup
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
// Step 2: Create frequency maps for vowels and consonants
const vowelFreq = {};