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 / minimumDifference.js
Created January 25, 2026 18:31
You are given a 0-indexed integer array nums, where nums[i] represents the score of the ith student. You are also given an integer k. Pick the scores of any k students from the array so that the difference between the highest and the lowest of the k
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minimumDifference = function(nums, k) {
// If k is 1, picking a single student always gives difference = 0
if (k === 1) return 0;
// Sort scores so close values sit next to each other
@tatsuyax25
tatsuyax25 / minPairSum.js
Created January 24, 2026 17:35
The pair sum of a pair (a,b) is equal to a + b. The maximum pair sum is the largest pair sum in a list of pairs. For example, if we have pairs (1,5), (2,3), and (4,4), the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8. Given an arr
/**
* @param {number[]} nums
* @return {number}
*/
var minPairSum = function(nums) {
// Step 1: Sort the array so we can pair smallest with largest
nums.sort((a, b) => a - b);
let left = 0; // pointer to smallest element
let right = nums.length - 1; // pointer largest element
@tatsuyax25
tatsuyax25 / minimumPairRemoval.js
Created January 23, 2026 18:50
Given an array nums, you can perform the following operation any number of times: Select the adjacent pair with the minimum sum in nums. If multiple such pairs exist, choose the leftmost one. Replace the pair with their sum. Return the minimum numbe
/**
* Simulates the "minimum adjacent sum merge" process efficiently using:
* - A doubly linked list (prev/next) to track alive indices
* - Versioning (ver[]) to invalidate stale heap entries
* - A custom typed min‑heap for fast pair selection
* - An inversion counter to detect when the array becomes non‑decreasing
*
* @param {number[]} nums
* @return {number}
*/
@tatsuyax25
tatsuyax25 / minBitwiseArray.js
Created January 21, 2026 18:23
You are given an array nums consisting of n prime integers. You need to construct an array ans of length n, such that, for each index i, the bitwise OR of ans[i] and ans[i] + 1 is equal to nums[i], i.e. ans[i] OR (ans[i] + 1) == nums[i]. Additional
/**
* @param {number[]} nums
* @return {number[]}
*/
var minBitwiseArray = function(nums) {
const ans = new Array(nums.length);
for (let i = 0; i < nums.length; i++) {
const p = nums[i];
@tatsuyax25
tatsuyax25 / minBitwiseArray.js
Created January 20, 2026 19:43
You are given an array nums consisting of n prime integers. You need to construct an array ans of length n, such that, for each index i, the bitwise OR of ans[i] and ans[i] + 1 is equal to nums[i], i.e. ans[i] OR (ans[i] + 1) == nums[i]. Additional
/**
* @param {number[]} nums
* @return {number[]}
*/
var minBitwiseArray = function(nums) {
const ans = new Array(nums.length);
for (let i = 0; i < nums.length; i++) {
const p = nums[i];
@tatsuyax25
tatsuyax25 / maxSideLength.js
Created January 19, 2026 16:17
Given a m x n matrix mat and an integer threshold, return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square.
/**
* @param {number[][]} mat
* @param {number} threshold
* @return {number}
*/
var maxSideLength = function(mat, threshold) {
const m = mat.length;
const n = mat[0].length;
// -----------------------------
@tatsuyax25
tatsuyax25 / largestMagicSquare.js
Created January 18, 2026 18:09
A k x k magic square is a k x k grid filled with integers such that every row sum, every column sum, and both diagonal sums are all equal. The integers in the magic square do not have to be distinct. Every 1 x 1 grid is trivially a magic square. Giv
/**
* @param {number[][]} grid
* @return {number}
*/
var largestMagicSquare = function(grid) {
const m = grid.length;
const n = grid[0].length;
// Helper: check if the k×k square starting at (r, c) is magic
function isMagic(r, c, k) {
@tatsuyax25
tatsuyax25 / largestSquareArea.js
Created January 17, 2026 17:24
There exist n rectangles in a 2D plane with edges parallel to the x and y axis. You are given two 2D integer arrays bottomLeft and topRight where bottomLeft[i] = [a_i, b_i] and topRight[i] = [c_i, d_i] represent the bottom-left and top-right coordina
/**
* @param {number[][]} bottomLeft
* @param {number[][]} topRight
* @return {number}
*/
var largestSquareArea = function(bottomLeft, topRight) {
let n = bottomLeft.length;
let maxArea = 0;
// Compare every pair of rectangles (i, j)
@tatsuyax25
tatsuyax25 / maximizeSquareArea.js
Created January 16, 2026 18:23
There is a large (m - 1) x (n - 1) rectangular field with corners at (1, 1) and (m, n) containing some horizontal and vertical fences given in arrays hFences and vFences respectively. Horizontal fences are from the coordinates (hFences[i], 1) to (hF
/**
* @param {number} m
* @param {number} n
* @param {number[]} hFences
* @param {number[]} vFences
* @return {number}
*/
var maximizeSquareArea = function(m, n, hFences, vFences) {
const MOD = 1_000_000_007;
@tatsuyax25
tatsuyax25 / maximizeSquareHoleArea.js
Created January 15, 2026 17:38
You are given the two integers, n and m and two integer arrays, hBars and vBars. The grid has n + 2 horizontal and m + 2 vertical bars, creating 1 x 1 unit cells. The bars are indexed starting from 1. You can remove some of the bars in hBars from ho
/**
* @param {number} n
* @param {number} m
* @param {number[]} hBars
* @param {number[]} vBars
* @return {number}
*/
var maximizeSquareHoleArea = function(n, m, hBars, vBars) {
/**
* Finds the longest run of consecutive integers in an array.