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 / new21Game.js
Created August 17, 2025 17:09
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points and draws numbers while she has less than k points. During each draw, she gains an integer number of points randomly from the range [1, maxPts], where ma
/**
* @param {number} n
* @param {number} k
* @param {number} maxPts
* @return {number}
*/
var new21Game = function(n, k, maxPts) {
// Edge case: if k == 0, Alice doesn't draw any cards, so score is 0 (always < n)
// Or if n >= k + maxPts, Alice can reach any score < n with certainty
if (k === 0 || n >= k + maxPts) return 1;
@tatsuyax25
tatsuyax25 / maximum69Number.js
Created August 16, 2025 17:52
You are given a positive integer num consisting only of digits 6 and 9. Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).
/**
* @param {number} num
* @return {number}
*/
var maximum69Number = function(num) {
// Convert the number into an array of digits
const digits = num.toString().split('');
let maxNum = num;
@tatsuyax25
tatsuyax25 / isPowerOfFour.js
Created August 15, 2025 16:20
Given an integer n, return true if it is a power of four. Otherwise, return false. An integer n is a power of four, if there exists an integer x such that n == 4x.
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfFour = function(n) {
// Powers of four must be positive
if (n <= 0) return false;
// Check if n is a power of two: only one bit is set
if ((n & (n - 1)) !== 0) return false;
@tatsuyax25
tatsuyax25 / largestGoodInteger.js
Created August 14, 2025 16:03
You are given a string num representing a large integer. An integer is good if it meets the following conditions: It is a substring of num with length 3. It consists of only one unique digit. Return the maximum good integer as a string or an empty s
/**
* @param {string} num
* @return {string}
*/
var largestGoodInteger = function(num) {
// Match all substrings of three identical digits using a regular expression
const matches = num.match(/(\d)\1{2}/g);
// If no matches are found, return an empty string
if (!matches) return '';
@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);