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 / minOperations.js
Created April 9, 2025 16:30
You are given an integer array nums and an integer k. An integer h is called valid if all values in the array that are strictly greater than h are identical. For example, if nums = [10, 8, 10, 8], a valid integer is h = 9 because all nums[i] > 9 ar
/**
* This function calculates the minimum operations required to satisfy certain conditions.
* @param {number[]} nums - Array of numbers to process.
* @param {number} k - Threshold value to compare against.
* @return {number} - Returns the size of the unique set of numbers greater than k, or -1 if any number is less than k.
*/
var minOperations = function (nums, k) {
// Check if any number in the array is less than k. If so, return -1.
for (let num of nums) {
if (num < k) return -1;
@tatsuyax25
tatsuyax25 / minimumOperations.js
Created April 8, 2025 17:49
You are given an integer array nums. You need to ensure that the elements in the array are distinct. To achieve this, you can perform the following operation any number of times: Remove 3 elements from the beginning of the array. If the array has fe
/**
* Finds the minimum number of operations required to make all elements in the array distinct
* by removing groups of 3 elements from the start of the array.
*
* @param {number[]} nums - The input array of integers.
* @return {number} - The minimum number of operations required.
*/
var minimumOperations = function(nums) {
// Create an array to track the elements that have already appeared
let appeared = new Array(101); // This seems to assume the numbers are in the range [0, 100].
@tatsuyax25
tatsuyax25 / canPartition.js
Created April 7, 2025 15:34
Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.
/**
* Determines if the given array of numbers can be partitioned into two subsets with equal sum.
* @param {number[]} nums - Array of integers
* @return {boolean} - True if the array can be partitioned, otherwise false
*/
var canPartition = function(nums) {
const total = nums.reduce((a, b) => a + b, 0);
// If the total is odd, partitioning into two equal subsets is not possible
if (total % 2 !== 0) return false;
@tatsuyax25
tatsuyax25 / largestDivisibleSubset.js
Created April 6, 2025 16:18
Given a set of distinct positive integers nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies: answer[i] % answer[j] == 0, or answer[j] % answer[i] == 0 If there are multiple soluti
/**
* Finds the largest subset of numbers from `nums` such that every pair (i, j)
* in the subset satisfies:
* nums[i] % nums[j] === 0 or nums[j] % nums[i] === 0.
*
* @param {number[]} nums - Array of distinct positive integers.
* @return {number[]} - The largest divisible subset.
*/
var largestDivisibleSubset = function(nums) {
// Edge case: If the input is empty, return an empty subset.
@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