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 / countGoodIntegers.js
Created April 12, 2025 16:55
You are given two positive integers n and k. An integer x is called k-palindromic if: x is a palindrome. x is divisible by k. An integer is called good if its digits can be rearranged to form a k-palindromic integer. For example, for k = 2, 2020 ca
/**
* @param {number} n
* @param {number} k
* @return {number}
*/
var countGoodIntegers = function(n, k) {
// Precalculate factorial values for combinatorics
const memoF = Array(n); // Memoization array for factorial values
memoF[0] = 1; // Base case: 0! = 1
for (let i = 1; i < n; i++) {
@tatsuyax25
tatsuyax25 / countSymmetricIntegers.js
Created April 11, 2025 16:35
You are given two positive integers low and high. An integer x consisting of 2 * n digits is symmetric if the sum of the first n digits of x is equal to the sum of the last n digits of x. Numbers with an odd number of digits are never symmetric. Re
/**
* @param {number} low
* @param {number} high
* @return {number}
*/
// Function to check if a number has an even number of digits
function hasEvenDigits(num) {
const digitCount = num.toString().length;
return digitCount % 2 === 0; // True if the digit count is even
}
@tatsuyax25
tatsuyax25 / numberOfPowerfulInt.js
Last active April 10, 2025 20:12
You are given three integers start, finish, and limit. You are also given a 0-indexed string s representing a positive integer. A positive integer x is called powerful if it ends with s (in other words, s is a suffix of x) and each digit in x is at
/**
* Calculates the number of "powerful integers" between the range `start` and `finish`.
* A "powerful integer" is determined by the given `limit` and string representation `s`.
*
* @param {number} start - Start of the range (inclusive).
* @param {number} finish - End of the range (inclusive).
* @param {number} limit - Maximum digit allowed in the calculation.
* @param {string} s - String representation of a threshold value.
* @return {number} - The number of powerful integers in the range.
*/
@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++) {