Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
:octocat:
Focusing

Miguel Urena tatsuyax25

:octocat:
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / isPowerOfThree.js
Created August 13, 2025 16:37
Given an integer n, return true if it is a power of three. Otherwise, return false. An integer n is a power of three, if there exists an integer x such that n == 3x.
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfThree = function(n) {
// Powers of 3 are always positive (3^0 = 1, 3^1 = 3, etc.)
if (n < 1) return false;
// Keep dividing n by 3 as long it's it's divisible
while (n % 3 === 0) {
@tatsuyax25
tatsuyax25 / numberOfWays.js
Created August 12, 2025 20:30
Given two positive integers n and x. Return the number of ways n can be expressed as the sum of the xth power of unique positive integers, in other words, the number of sets of unique integers [n1, n2, ..., nk] where n = n1x + n2x + ... + nkx. Sinc
/**
* @param {number} n
* @param {number} x
* @return {number}
*/
var numberOfWays = function(n, x) {
const MOD = 1e9 + 7;
// Memoization cache: key = `${total},${current}`
const memo = new Map();
@tatsuyax25
tatsuyax25 / productQueries.js
Created August 11, 2025 15:42
Given a positive integer n, there exists a 0-indexed array called powers, composed of the minimum number of powers of 2 that sum to n. The array is sorted in non-decreasing order, and there is only one way to form the array. You are also given a 0-i
/**
* @param {number} n
* @param {number[][]} queries
* @return {number[]}
*/
var productQueries = function(n, queries) {
const MOD = 1e9 + 7;
// Step 1: Decompose n into powers of 2 using its binary representation
const powers = [];
@tatsuyax25
tatsuyax25 / reorderedPowerOf2.js
Created August 10, 2025 17:40
You are given an integer n. We reorder the digits in any order (including the original order) such that the leading digit is not zero. Return true if and only if we can do this so that the resulting number is a power of two.
/**
* @param {number} n
* @return {boolean}
*/
var reorderedPowerOf2 = function(n) {
// Convert the number to an array of digit characters
const digits = n.toString().split('');
// Helper function to generate all unique permutations of the digits
function getPermutations(arr) {
@tatsuyax25
tatsuyax25 / isPowerOfTwo.js
Created August 9, 2025 16:59
Given an integer n, return true if it is a power of two. Otherwise, return false. An integer n is a power of two, if there exists an integer x such that n == 2x.
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfTwo = function(n) {
// Powers of two must be positive integers greater than zero
if (n < 1) {
return false;
}
@tatsuyax25
tatsuyax25 / soupServings.js
Last active August 8, 2025 18:14
You have two soups, A and B, each starting with n mL. On every turn, one of the following four serving operations is chosen at random, each with probability 0.25 independent of all previous turns: pour 100 mL from type A and 0 mL from type B pour 75
/**
* @param {number} n
* @return {number}
*/
var soupServings = function(n) {
// Optimization: for large n, the probability approaches 1
if (n >= 4800) return 1.0;
// Convert mL to units of 25 mL to reduce state space
const N = Math.ceil(n / 25);
@tatsuyax25
tatsuyax25 / maxCollectedFruits.js
Created August 7, 2025 16:43
There is a game dungeon comprised of n x n rooms arranged in a grid. You are given a 2D array fruits of size n x n, where fruits[i][j] represents the number of fruits in the room (i, j). Three children will play in the game dungeon, with initial pos
/**
* @param {number[][]} fruits - 2D grid representing fruits in each room
* @return {number} - Maximum fruits collected by the three children
*/
var maxCollectedFruits = function(fruits) {
const n = fruits.length;
// Step 1: Sum the main diagonal fruits (child A from top-left to bottom-right)
let diagonalSum = 0;
for (let i = 0; i < n; i++) {
@tatsuyax25
tatsuyax25 / numOfUnplacedFruits.js
Last active September 4, 2025 18:49
You are given two arrays of integers, fruits and baskets, each of length n, where fruits[i] represents the quantity of the ith type of fruit, and baskets[j] represents the capacity of the jth basket. From left to right, place the fruits according to
/**
* Counts how many fruit types cannot be placed in any basket.
* Each fruit must go into the leftmost available basket with enough capacity.
* Each basket can hold only one fruit type.
*
* @param {number[]} fruits - Array of fruit quantities.
* @param {number[]} baskets - Array of basket capacities.
* @return {number} - Number of unplaced fruit types.
*/
var numOfUnplacedFruits = function(fruits, baskets) {
@tatsuyax25
tatsuyax25 / numOfUnplacedFruits.js
Last active September 4, 2025 18:49
You are given two arrays of integers, fruits and baskets, each of length n, where fruits[i] represents the quantity of the ith type of fruit, and baskets[j] represents the capacity of the jth basket. From left to right, place the fruits according to
/**
* @param {number[]} fruits
* @param {number[]} baskets
* @return {number}
*/
var numOfUnplacedFruits = function(fruits, baskets) {
// Track basket usage using a Set to store used indices
const usedBaskets = new Set();
// Track number of unplaced fruits
@tatsuyax25
tatsuyax25 / totalFruit.js
Created August 4, 2025 19:00
You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.
/**
* @param {number[]} fruits
* @return {number}
*/
var totalFruit = function(fruits) {
let maxFruits = 0;
let left = 0;
const basket = new Map(); // Holds fruit type and count
// Move the right boundary of the window