π¨βπ»
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
/** | |
* Function to calculate the maximum points that can be earned | |
* by answering questions strategically. | |
* @param {number[][]} questions - Array of questions, where each question is represented as [points, brainpower]. | |
* @return {number} - The maximum points that can be achieved. | |
*/ | |
var mostPoints = function(questions) { | |
const n = questions.length; | |
// Memoization array to store results of subproblems |
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
/** | |
* Calculates the difference between the maximum and minimum scores | |
* of distributing marbles into k bags based on given rules. | |
* | |
* @param {number[]} weights - Array representing the weights of marbles. | |
* @param {number} k - Number of bags to divide the marbles into. | |
* @return {number} - Difference between the maximum and minimum scores. | |
*/ | |
var putMarbles = function(weights, k) { | |
// If there's only one bag, no need to calculate; the difference is 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
// Function to get the index of a character in the alphabet (0 for 'a', 1 for 'b', etc.) | |
const getIndexCharacter = (c) => { | |
return c.charCodeAt(0) - 'a'.charCodeAt(0); | |
} | |
/** | |
* Function to partition the string into as many parts as possible | |
* such that each character appears in at most one part. | |
* | |
* @param {string} s - Input string |
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 | |
* @param {number} k | |
* @return {number} | |
*/ | |
// A class to handle modular arithmetic operations | |
class Modulo { | |
constructor(modulo) { | |
this.modulo = modulo; // The modulus for all operations | |
this._phi = modulo - 1; // Euler's totient function for primes (modulo is prime) |
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[][]} grid | |
* @param {number[]} queries | |
* @return {number[]} | |
*/ | |
// Main function to compute maximum points for each query | |
var maxPoints = function(grid, queries) { | |
let m = grid.length, n = grid[0].length; | |
// Store all grid cells as coordinates with their values |
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
/** | |
* Finds the minimum index to split the array such that both subarrays | |
* have the same dominant element. | |
* | |
* @param {number[]} nums - The input array of integers. | |
* @return {number} - The minimum index of a valid split, or -1 if no valid split exists. | |
*/ | |
var minimumIndex = function(nums) { | |
const n = nums.length; |
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
/** | |
* This function calculates the minimum number of operations needed | |
* to transform a 2D grid into a uniform grid based on a given step size. | |
* If it's not possible, it returns -1. | |
* | |
* @param {number[][]} grid - The input 2D grid of numbers. | |
* @param {number} x - The step size for allowed operations. | |
* @return {number} The minimum number of operations, or -1 if not possible. | |
*/ | |
var minOperations = function(grid, x) { |
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
/** | |
* Function to check if two horizontal or two vertical cuts can divide the grid | |
* into three sections, with each section containing at least one rectangle. | |
* @param {number} n - The dimensions of the n x n grid (not directly used here). | |
* @param {number[][]} rectangles - Array of rectangles where each rectangle is defined as [startx, starty, endx, endy]. | |
* @return {boolean} - Returns true if valid cuts can be made; otherwise, false. | |
*/ | |
var checkValidCuts = function(n, rectangles) { | |
// Arrays to store x and y intervals for each rectangle | |
let x = []; |
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
/** | |
* Function to calculate the number of days an employee is free from meetings. | |
* | |
* @param {number} days - Total number of days the employee is available for work. | |
* @param {number[][]} meetings - Array of meeting intervals where each meeting is [start, end]. | |
* @returns {number} - The count of days when the employee is free from meetings. | |
*/ | |
var countDays = function(days, meetings) { | |
// Sort meetings by their start day to process them in chronological order | |
meetings.sort((a, b) => a[0] - b[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 - Number of intersections (nodes) | |
* @param {number[][]} roads - 2D array representing roads where [a, b, time] means | |
* a road exists between a and b with travel time "time" | |
* @return {number} - Number of ways to reach intersection (n-1) in the shortest time | |
*/ | |
var countPaths = function(n, roads) { | |
const MOD = 1_000_000_007; // Modular arithmetic base | |
// PART ONE: Build the graph and calculate shortest distances |