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 / maxDistance.js
Created June 20, 2025 16:41
You are given a string s consisting of the characters 'N', 'S', 'E', and 'W', where s[i] indicates movements in an infinite grid: 'N' : Move north by 1 unit. 'S' : Move south by 1 unit. 'E' : Move east by 1 unit. 'W' : Move west by 1 unit. Initially
/**
* Calculates the maximum Manhattan distance from the origin
* that can be achieved while moving in a grid, with the option
* to change up to 'k' directions.
*
* @param {string} s - String of movement directions: 'N', 'S', 'E', 'W'.
* @param {number} k - Number of allowed direction changes.
* @return {number} - Maximum distance from the origin at any point.
*/
var maxDistance = function(s, k) {
@tatsuyax25
tatsuyax25 / partitionArray.js
Created June 19, 2025 01:19
You are given an integer array nums and an integer k. You may partition nums into one or more subsequences such that each element in nums appears in exactly one of the subsequences. Return the minimum number of subsequences needed such that the diff
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var partitionArray = function(nums, k) {
if (nums.length === 0) return 0;
// Step 1: Sort the array so that we can group close numbers together
nums.sort((a, b) => a - b);
@tatsuyax25
tatsuyax25 / divideArray
Created June 18, 2025 18:36
You are given an integer array nums of size n where n is a multiple of 3 and a positive integer k. Divide the array nums into n / 3 arrays of size 3 satisfying the following condition: The difference between any two elements in one array is less th
/**
* @param {number[]} nums
* @param {number} k
* @return {number[][]}
*/
// Function to divide an array into subarrays of size 3
// such that the difference between the smallest and largest element in each subarray is less than or equal to k
var divideArray = function(nums, k) {
const arraySize = 3; // Size of each subarray
const result = []; // Array to store the result
@tatsuyax25
tatsuyax25 / countGoodArrays.js
Created June 17, 2025 18:55
You are given three integers n, m, k. A good array arr of size n is defined as follows: Each element in arr is in the inclusive range [1, m]. Exactly k indices i (where 1 <= i < n) satisfy the condition arr[i - 1] == arr[i]. Return the number of goo
/**
* @param {number} n
* @param {number} m
* @param {number} k
* @return {number}
*/
var countGoodArrays = function(n, m, k) {
const MOD = 1e9 + 7; // Large prime number for modulo operations to prevent overflow
// Function to compute (a^b) % MOD using binary exponentiation (efficient power calculation)
@tatsuyax25
tatsuyax25 / maximumDifference.js
Created June 16, 2025 20:38
Given a 0-indexed integer array nums of size n, find the maximum difference between nums[i] and nums[j] (i.e., nums[j] - nums[i]), such that 0 <= i < j < n and nums[i] < nums[j]. Return the maximum difference. If no such i and j exists, return -1.
/**
* @param {number[]} nums
* @return {number}
*/
var maximumDifference = function(nums) {
// Edge case: If the array has fewer than 2 elements, there's no valid pair.
if (nums.length < 2) return -1;
let minSoFar = nums[0]; // Track the smallest element seen so far
let maxDiff = -1; // Initialize max difference to -1 (default if no valid pair is found)
@tatsuyax25
tatsuyax25 / maxDiff.js
Last active June 15, 2025 18:02
You are given an integer num. You will apply the following steps to num two separate times: Pick a digit x (0 <= x <= 9). Pick another digit y (0 <= y <= 9). Note y can be equal to x. Replace all the occurrences of x in the decimal representation of
/**
* @param {number} num
* @return {number}
*/
var maxDiff = function(num) {
let numStr = num.toString(); // Convert number to string for easy mainpulation
// **Step 1: Find the maximum number by replacing the first non-9 digit with 9**
let maxNumStr = numStr;
@tatsuyax25
tatsuyax25 / minMaxDifference.js
Created June 14, 2025 16:30
You are given an integer num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit. Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in num. Notes: Wh
/**
* @param {number} num
* @return {number}
*/
var minMaxDifference = function(num) {
let numStr = num.toString();
// Finding the max number
let maxDigit = numStr.split('').find(d => d !== '9'); // First non-9 digit
let maxNum = parseInt(numStr.replaceAll(maxDigit, '9'), 10);
@tatsuyax25
tatsuyax25 / minimizeMax.js
Created June 13, 2025 15:42
You are given a 0-indexed integer array nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs. Note that for a pai
/**
* Function to minimize the maximum difference between pairs in a sorted array.
* @param {number[]} nums - Array of numbers to be paired.
* @param {number} p - Target number of pairs.
* @return {number} - Minimum possible maximum difference between pairs.
*/
var minimizeMax = function(nums, p) {
// Sort the numbers in ascending order
nums.sort((a, b) => a - b);
@tatsuyax25
tatsuyax25 / maxAdjacentDistance.js
Created June 12, 2025 17:14
Given a circular array nums, find the maximum absolute difference between adjacent elements. Note: In a circular array, the first and last elements are adjacent.
/**
* @param {number[]} nums
* @return {number}
*/
var maxAdjacentDistance = function(nums) {
if (nums.length < 2) return 0; // If there's only one element, no adjacent pairs exist.
let maxDiff = 0; // Initialize a variable to store the maximum absolute difference
for (let i = 0; i < nums.length; i++) {
@tatsuyax25
tatsuyax25 / maxDifference.js
Created June 11, 2025 17:12
You are given a string s and an integer k. Your task is to find the maximum difference between the frequency of two characters, freq[a] - freq[b], in a substring subs of s, such that: subs has a size of at least k. Character a has an odd frequency i
/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var maxDifference = function(s, k) {
const n = s.length;
// Convert each character to a digit (0-4 for faster lookup)
const digits = new Uint8Array(n);