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 / finalValueAfterOperations.js
Created October 20, 2025 15:22
There is a programming language with only four operations and one variable X: ++X and X++ increments the value of the variable X by 1. --X and X-- decrements the value of the variable X by 1. Initially, the value of X is 0. Given an array of string
/**
* @param {string[]} operations
* @return {number}
*/
var finalValueAfterOperations = function(operations) {
// Step 1: Initialize the variable X to 0
let X = 0;
// Step 2: Loop through each operation in the array
for (let i = 0; i < operations.length; i++) {
@tatsuyax25
tatsuyax25 / findLexSmallestString.js
Created October 19, 2025 17:01
You are given a string s of even length consisting of digits from 0 to 9, and two integers a and b. You can apply either of the following two operations any number of times and in any order on s: Add a to all odd indices of s (0-indexed). Digits po
/**
* @param {string} s
* @param {number} a
* @param {number} b
* @return {string}
*/
var findLexSmallestString = function(s, a, b) {
// Set to keep track of visited strings to avoid revisiting the same state
const visited = new Set();
@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)