Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
πŸ‘¨β€πŸ’»
Focusing

Miguel Urena tatsuyax25

πŸ‘¨β€πŸ’»
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / mostPoints.js
Created April 1, 2025 20:30
You are given a 0-indexed 2D integer array questions where questions[i] = [pointsi, brainpoweri]. The array describes the questions of an exam, where you have to process the questions in order (i.e., starting from question 0) and make a decision whe
/**
* 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
@tatsuyax25
tatsuyax25 / putMarbles.js
Created March 31, 2025 20:17
You have k bags. You are given a 0-indexed integer array weights where weights[i] is the weight of the ith marble. You are also given the integer k. Divide the marbles into the k bags according to the following rules: No bag is empty. If the ith ma
/**
* 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.
@tatsuyax25
tatsuyax25 / partitionLabels.js
Created March 30, 2025 17:37
You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part. For example, the string "ababcc" can be partitioned into ["abab", "cc"], but partitions such as ["aba", "bcc"] o
// 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
@tatsuyax25
tatsuyax25 / maximumScore.js
Created March 29, 2025 16:26
You are given an array nums of n positive integers and an integer k. Initially, you start with a score of 1. You have to maximize your score by applying the following operation at most k times: Choose any non-empty subarray nums[l, ..., r] that you
/**
* @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)
@tatsuyax25
tatsuyax25 / maxPoints.js
Created March 28, 2025 15:46
You are given an m x n integer matrix grid and an array queries of size k. Find an array answer of size k such that for each integer queries[i] you start in the top left cell of the matrix and repeat the following process: If queries[i] is strictly
/**
* @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
@tatsuyax25
tatsuyax25 / minimumIndex.js
Created March 27, 2025 20:04
An element x of an integer array arr of length m is dominant if more than half the elements of arr have a value of x. You are given a 0-indexed integer array nums of length n with one dominant element. You can split nums at an index i into two arra
/**
* 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;
@tatsuyax25
tatsuyax25 / minOperations.js
Created March 26, 2025 21:38
You are given a 2D integer grid of size m x n and an integer x. In one operation, you can add x to or subtract x from any element in the grid. A uni-value grid is a grid where all the elements of it are equal. Return the minimum number of operation
/**
* 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) {
@tatsuyax25
tatsuyax25 / checkValidCuts.js
Created March 25, 2025 20:32
You are given an integer n representing the dimensions of an n x n grid, with the origin at the bottom-left corner of the grid. You are also given a 2D array of coordinates rectangles, where rectangles[i] is in the form [startx, starty, endx, endy],
/**
* 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 = [];
@tatsuyax25
tatsuyax25 / countDays.js
Created March 24, 2025 19:05
You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, meetings[i] = [start_i, end_i] represents the starting and en
/**
* 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]);
@tatsuyax25
tatsuyax25 / countPaths.js
Created March 23, 2025 17:49
You are in a city that consists of n intersections numbered from 0 to n - 1 with bi-directional roads between some intersections. The inputs are generated such that you can reach any intersection from any other intersection and that there is at most
/**
* @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