π¨βπ»
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
/** | |
* 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 |
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 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); |
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 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++) { |
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) { |