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 / maxPartitionsAfterOperations.js
Created October 17, 2025 16:07
You are given a string s and an integer k. First, you are allowed to change at most one index in s to another lowercase English letter. After that, do the following partitioning operation until s is empty: Choose the longest prefix of s containing
/**
* Calculates the maximum number of partitions after performing operations
* that allow up to `k` distinct characters per partition.
*
* @param {string} s - Input string consisting of lowercase letters.
* @param {number} k - Maximum number of distinct characters allowed per partition.
* @return {number} - Maximum number of partitions achievable.
*/
var maxPartitionsAfterOperations = function(s, k) {
const n = s.length;
@tatsuyax25
tatsuyax25 / findSmallestInteger.js
Created October 16, 2025 16:55
You are given a 0-indexed integer array nums and an integer value. In one operation, you can add or subtract value from any element of nums. For example, if nums = [1,2,3] and value = 2, you can choose to subtract value from nums[0] to make nums =
/**
* @param {number[]} nums
* @param {number} value
* @return {number}
*/
var findSmallestInteger = function(nums, value) {
// Step 1: Create a frequency map to count how many times each remainder appears
const freq = new Map();
for (let num of nums) {
@tatsuyax25
tatsuyax25 / maxIncreasingSubarrays.js
Last active October 15, 2025 16:45
Given an array nums of n integers, your task is to find the maximum value of k for which there exist two adjacent subarrays of length k each, such that both subarrays are strictly increasing. Specifically, check if there are two subarrays of length k
/**
* @param {number[]} nums
* @return {number}
*/
var maxIncreasingSubarrays = function (nums) {
let maxK = 0; // Stores the maximum valid k found so far
let curLen = 1; // Length of the current strictly increasing run
let prevLen = 0; // Length of the previous strictly increasing run
for (let i = 1; i < nums.length; i++) {
@tatsuyax25
tatsuyax25 / hasIncreasingSubarrays.js
Created October 14, 2025 20:25
Given an array nums of n integers and an integer k, determine whether there exist two adjacent subarrays of length k such that both subarrays are strictly increasing. Specifically, check if there are two subarrays starting at indices a and b (a < b),
/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var hasIncreasingSubarrays = function(nums, k) {
// Helper function to check if a subarray is strictly increasing
function isStrictlyIncreasing(start, end) {
for (let i = start; i < end; i++) {
if (nums[i] >= nums[i + 1]) {
@tatsuyax25
tatsuyax25 / removeAnagrams.js
Created October 13, 2025 17:13
You are given a 0-indexed string array words, where words[i] consists of lowercase English letters. In one operation, select any index i such that 0 < i < words.length and words[i - 1] and words[i] are anagrams, and delete words[i] from words. Keep
/**
* @param {string[]} words
* @return {string[]}
*/
var removeAnagrams = function(words) {
// Helper function to check if two words are anagrams
const isAnagram = (word1, word2) => {
// Sort the letters of both words and compare
return word1.split('').sort().join('') === word2.split('').sort().join('');
};
@tatsuyax25
tatsuyax25 / magicalSum.js
Last active October 12, 2025 17:02
You are given two integers, m and k, and an integer array nums. A sequence of integers seq is called magical if: seq has a size of m. 0 <= seq[i] < nums.length The binary representation of 2seq[0] + 2seq[1] + ... + 2seq[m - 1] has k set bits. The ar
/**
* @param {number} m
* @param {number} k
* @param {number[]} nums
* @return {number}
*/
var magicalSum = function(m, k, nums) {
const MOD = BigInt(1e9 + 7); // Modulo for large number arithmetic
const n = nums.length;
@tatsuyax25
tatsuyax25 / maximumTotalDamage.js
Created October 11, 2025 16:57
A magician has various spells. You are given an array power, where each element represents the damage of a spell. Multiple spells can have the same damage value. It is a known fact that if a magician decides to cast a spell with a damage of power[i
/**
* @param {number[]} power
* @return {number}
*/
var maximumTotalDamage = function(power) {
// Step 1: Count how many times each damage value appears
const count = new Map();
for (const dmg of power) {
count.set(dmg, (count.get(dmg) || 0) + 1);
}
@tatsuyax25
tatsuyax25 / maximumEnergy.js
Created October 10, 2025 18:56
In a mystic dungeon, n magicians are standing in a line. Each magician has an attribute that gives you energy. Some magicians can give you negative energy, which means taking energy from you. You have been cursed in such a way that after absorbing e
/**
* @param {number[]} energy
* @param {number} k
* @return {number}
*/
var maximumEnergy = function(energy, k) {
// Initialize the answer to a very low number to ensure any valid energy path will be higher
let ans = -1e99;
// Try starting from each index i in the range [0, k)
@tatsuyax25
tatsuyax25 / minTime.js
Created October 9, 2025 18:51
You are given two integer arrays, skill and mana, of length n and m, respectively. In a laboratory, n wizards must brew m potions in order. Each potion has a mana capacity mana[j] and must pass through all the wizards sequentially to be brewed prope
/**
* @param {number[]} skill
* @param {number[]} mana
* @return {number}
*/
var minTime = function(skill, mana) {
let m = mana.length; // Number of potions
let n = skill.length; // Number of wizards
// done[i] represents the time when wizard i is ready to start the next potion
@tatsuyax25
tatsuyax25 / successfulPairs.js
Created October 8, 2025 16:41
You are given two positive integer arrays spells and potions, of length n and m respectively, where spells[i] represents the strength of the ith spell and potions[j] represents the strength of the jth potion. You are also given an integer success. A
/**
* @param {number[]} spells
* @param {number[]} potions
* @param {number} success
* @return {number[]}
*/
// Helper function to preprocess potions using a suffix-style bucket sort
var suffixBucketSort = function(potions) {
// Find the maximum potion value to size the result array