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 / lcaDeepestLeaves.js
Created April 4, 2025 16:15
Given the root of a binary tree, return the lowest common ancestor of its deepest leaves. Recall that: The node of a binary tree is a leaf if and only if it has no children The depth of the root of the tree is 0. if the depth of a node is d, the de
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
@tatsuyax25
tatsuyax25 / maximumTripletValue.js
Created April 3, 2025 17:57
You are given a 0-indexed integer array nums. Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0. The value of a triplet of indices (i, j, k) is equal to (nums[i
/**
* @param {number[]} nums
* @return {number}
*/
var maximumTripletValue = function(nums) {
const n = nums.length;
if (n < 3) return 0; // Edge case: no valid triplet possible
// Arrays to store the maximum values
const leftMax = new Array(n).fill(Number.MIN_SAFE_INTEGER);
@tatsuyax25
tatsuyax25 / maximumTripletValue.js
Created April 2, 2025 17:32
You are given a 0-indexed integer array nums. Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0. The value of a triplet of indices (i, j, k) is equal to (nums[i
/**
* @param {number[]} nums
* @return {number}
*/
var maximumTripletValue = function(nums) {
// Initialize the maximum value to 0 (default if all values are negative)
let maxValue = 0;
// Outer loop: Fix the first index i
for (let i = 0; i < nums.length - 2; i++) {
@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) {