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 / minOperations.js
Created September 6, 2025 14:51
You are given a 2D array queries, where queries[i] is of the form [l, r]. Each queries[i] defines an array of integers nums consisting of elements ranging from l to r, both inclusive. In one operation, you can: Select two integers a and b from the
/**
* Calculates the minimum number of operations required to reduce
* all elements in each query range [l, r] to zero.
*
* In one operation, select two numbers and replace them with floor(a / 4) and floor(b / 4).
*
* This solution groups numbers by their "division depth" — how many times
* they must be divided by 4 before reaching 0 — and counts how many numbers
* fall into each depth level.
*
@tatsuyax25
tatsuyax25 / makeTheIntegerZero.js
Created September 5, 2025 18:49
You are given two integers num1 and num2. In one operation, you can choose integer i in the range [0, 60] and subtract 2i + num2 from num1. Return the integer denoting the minimum number of operations needed to make num1 equal to 0. If it is impos
/**
* Determines the minimum number of operations needed to reduce num1 to 0.
* Each operation subtracts (2^i + num2), where i ∈ [0, 60].
* Returns -1 if it's impossible.
*
* @param {number} num1 - Starting value
* @param {number} num2 - Fixed offset added to each power of 2
* @return {number} - Minimum number of operations or -1
*/
var makeTheIntegerZero = function(num1, num2) {
@tatsuyax25
tatsuyax25 / findClosest.js
Created September 4, 2025 18:44
You are given three integers x, y, and z, representing the positions of three people on a number line: x is the position of Person 1. y is the position of Person 2. z is the position of Person 3, who does not move. Both Person 1 and Person 2 move to
/**
* @param {number} x
* @param {number} y
* @param {number} z
* @return {number}
*/
var findClosest = function(x, y, z) {
// Calculate absolute distance from Person 1 to Person 3
const distance1 = Math.abs(x - z);
@tatsuyax25
tatsuyax25 / numberOfPairs.js
Created September 3, 2025 18:34
You are given a 2D array points of size n x 2 representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi]. We define the right direction as positive x-axis (increasing x-coordinate) and the left direction as negative x-
/**
* Counts the number of valid point pairs (i, j) such that:
* - x[i] ≤ x[j]
* - y[i] ≥ y[j]
* - No other point between i and j has the same or higher y[j]
*
* @param {number[][]} points - Array of [x, y] coordinates
* @return {number} - Number of valid pairs
*/
var numberOfPairs = function(points) {
@tatsuyax25
tatsuyax25 / numberOfPairs.js
Created September 2, 2025 16:06
You are given a 2D array points of size n x 2 representing integer coordinates of some points on a 2D plane, where points[i] = [xi, yi]. Count the number of pairs of points (A, B), where A is on the upper left side of B, and there are no other poin
/**
* @param {number[][]} points
* @return {number}
*/
var numberOfPairs = function(points) {
let count = 0;
// Step 1: Sort points by x ascending, then y descending
// This ensures that for any point[i], all point[j] with j < i have x <= x[i]
points.sort((a, b) => {
@tatsuyax25
tatsuyax25 / solveSudoku.js
Created August 31, 2025 16:12
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each colum
/**
* @param {character[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
var solveSudoku = function(board) {
const SIZE = 9;
// Track used digits in rows, columns, and 3x3 boxes
const row = Array.from({ length: SIZE }, () => Array(SIZE).fill(false));
const col = Array.from({ length: SIZE }, () => Array(SIZE).fill(false));
@tatsuyax25
tatsuyax25 / isValidSudoku.js
Created August 30, 2025 17:23
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of the
/**
* Validates a partially filled 9x9 Sudoku board.
* Only filled cells are checked for rule violations.
*
* @param {character[][]} board - 2D array representing the Sudoku board
* @return {boolean} - true if the board is valid, false otherwise
*/
var isValidSudoku = function(board) {
// Set to track seen digits in rows, columns, and sub-boxes
const seen = new Set();
@tatsuyax25
tatsuyax25 / flowerGame.js
Created August 29, 2025 17:05
Alice and Bob are playing a turn-based game on a field, with two lanes of flowers between them. There are x flowers in the first lane between Alice and Bob, and y flowers in the second lane between them. The game proceeds as follows: Alice takes th
/**
* @param {number} n
* @param {number} m
* @return {number}
*/
var flowerGame = function(n, m) {
// Count how many odd numbers are in [1, n]
const oddX = Math.floor(n / 2) + (n % 2); // e.g., 1, 3, 5...
const evenX = Math.floor(n / 2); // e.g., 2, 4, 6...
@tatsuyax25
tatsuyax25 / sortMatrix.js
Created August 28, 2025 16:28
You are given an n x n square matrix of integers grid. Return the matrix such that: The diagonals in the bottom-left triangle (including the middle diagonal) are sorted in non-increasing order. The diagonals in the top-right triangle are sorted in n
/**
* @param {number[][]} grid
* @return {number[][]}
*/
var sortMatrix = function(grid) {
const n = grid.length;
// Helper to extract a matrix starting at (row, col)
function getMatrix(row, col) {
const matrix = [];
@tatsuyax25
tatsuyax25 / lenOfVDiagonal.js
Created August 27, 2025 17:17
You are given a 2D integer matrix grid of size n x m, where each element is either 0, 1, or 2. A V-shaped diagonal segment is defined as: The segment starts with 1. The subsequent elements follow this infinite sequence: 2, 0, 2, 0, .... The segment
/**
* @param {number[][]} grid
* @return {number}
*/
var lenOfVDiagonal = function(grid) {
const n = grid.length;
const m = grid[0].length;
// Diagonal directions: ↘, ↙, ↖, ↗
const DIRS = [