This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number} numerator | |
* @param {number} denominator | |
* @return {string} | |
*/ | |
var fractionToDecimal = function(numerator, denominator) { | |
if (numerator === 0) return "0"; | |
let result = ""; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); | |
}; | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |