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 / 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
@tatsuyax25
tatsuyax25 / avoidFlood.js
Created October 7, 2025 17:52
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake that is full of water, there will be a flood. Your goal is to avoid flo
/**
* @param {number[]} rains
* @return {number[]}
*/
var avoidFlood = function(rains) {
const n = rains.length;
const ans = new Array(n).fill(1); // default dry days to lake 1 (will be overwritten)
const fullLakes = new Map(); // maps lake number to the day it was last filled
const dryDays = []; // stores indices of dry days
@tatsuyax25
tatsuyax25 / swimInWater.js
Created October 6, 2025 18:16
You are given an n x n integer matrix grid where each value grid[i][j] represents the elevation at that point (i, j). It starts raining, and water gradually rises over time. At time t, the water level is t, meaning any cell with elevation less than
/**
* @param {number[][]} grid - A 2D grid representing elevations at each cell.
* @return {number} - Minimum time required to reach bottom-right corner from top-left.
*/
var swimInWater = function (grid) {
const n = grid.length; // Grid is n x n
const dirs = [[1,0],[-1,0],[0,1],[0,-1]]; // Possible movement directions: down, up, right, left
// Custom MinHeap class to prioritize cells with the lowest time (elevation) first
class MinHeap {
@tatsuyax25
tatsuyax25 / pacificAtlantic.js
Created October 5, 2025 14:48
There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges. The island is partitioned int
/**
* Given a matrix of heights, returns coordinates of cells that can flow to both the Pacific and Atlantic oceans.
* Pacific touches the left and top edges; Atlantic touches the right and bottom edges.
* @param {number[][]} heights - 2D grid of elevation values
* @return {number[][]} - List of coordinates [i, j] that can reach both oceans
*/
var pacificAtlantic = function(heights) {
let row = heights.length;
let col = heights[0].length;
let arr = []; // Stores final result: cells that can reach both oceans