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 / 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.
@tatsuyax25
tatsuyax25 / separateSquares.js
Created January 14, 2026 18:31
You are given a 2D integer array squares. Each squares[i] = [xi, yi, li] represents the coordinates of the bottom-left point and the side length of a square parallel to the x-axis. Find the minimum y-coordinate value of a horizontal line such that t
/**
* @param {number[][]} squares
* @return {number}
*/
var separateSquares = function (squares) {
// ------------------------------------------------------------
// BUILD SWEEP EVENTS + COLLECT ALL X-COORDINATES
// ------------------------------------------------------------
// For each square:
@tatsuyax25
tatsuyax25 / separateSquares.js
Last active January 13, 2026 17:56
You are given a 2D integer array squares. Each squares[i] = [xi, yi, li] represents the coordinates of the bottom-left point and the side length of a square parallel to the x-axis. Find the minimum y-coordinate value of a horizontal line such that t
/**
* @param {number[][]} squares
* @return {number}
*/
var separateSquares = function (squares) {
// ------------------------------------------------------------
// STEP 1: Compute total area (counting overlaps multiple times)
// ------------------------------------------------------------
let totalArea = 0;
for (const [x, y, l] of squares) {
@tatsuyax25
tatsuyax25 / minTimeToVisitAllPoints.js
Created January 12, 2026 19:41
On a 2D plane, there are n points with integer coordinates points[i] = [xi, yi]. Return the minimum time in seconds to visit all the points in the order given by points. You can move according to these rules: In 1 second, you can either: move verti
/**
* @param {number[][]} points
* @return {number}
*/
var minTimeToVisitAllPoints = function(points) {
// Total time accumulator
let totalTime = 0;
// Iterate through each consecutive pair of points
for (let i = 1; i < points.length; i++) {